Exercise 1.7: Promises & Async/Await
Level 1
Exercise 1: Create a promise that resolves after 2 seconds and returns "Hello, world"
.
Exercise 2: Use .then
to display the result.
Exercise 3: Promise with reject:
Create a promise that resolves after 2 seconds if the input equals 'Hello', and rejects if the input is anything else.
Exercise 4: Async function with await
: Write an async function that uses await to wait for the result of the promise created in exercise 1, then logs that result to the console.
Level 2
Exercise 5: Error handling with async/await:
Modify the function from exercise 4 to catch any possible errors using a try/catch block.
Level 3
Exercise 6: Promise.all
: Create two promises that resolve after 2 and 3 seconds respectively. Use Promise.all to wait for both promises to resolve, then log the results to the console.
⚠️ Important: Open the browser console (F12 or Ctrl+Shift+I) to see the results of the exercises and verify that they work correctly.