w3resource

Explore JavaScript Promises and Async/Await: Exercises with Solutions


JavaScript Promises and Async/Await [10 exercises with solutions]

Create a Simple Promise:

1. Write a JavaScript function that returns a Promise that resolves with a "Hello, World!" message after 1 second.

Click me to see the solution

Promise Chain Example:

2. Write a JavaScript function that uses a chain of .then() calls to perform a series of asynchronous tasks.

Click me to see the solution

Handling Promise Rejection:

3. Write a JavaScript program that demonstrates how to catch and handle errors in Promises using .catch().

Click me to see the solution

Promise.any() Example:

4. Write a JavaScript function that takes multiple Promises and resolves with the first successful result using Promise.any().

Click me to see the solution

Promise.allSettled() Example:

5. Write a JavaScript program that takes an array of Promises and logs both resolved and rejected results using Promise.allSettled().

Click me to see the solution

Simulating a Task Queue:

6. Write a JavaScript program that processes an array of asynchronous tasks sequentially using Promises.

Click me to see the solution

Chained Async/Await Example:

7. Write a JavaScript program that uses async/await to perform three asynchronous tasks in sequence.

Click me to see the solution

Error Handling in Async/Await:

8. Write a JavaScript program to demonstrate how to handle errors in async/await functions using try...catch.

Click me to see the solution

Simulating a Throttled API:

9. Write a JavaScript function that ensures only a specified number of asynchronous requests are made simultaneously.

Click me to see the solution

Custom Promise Wrapper:

10. Write a JavaScript function that wraps an asynchronous operation in a Promise.

Click me to see the solution

Live Demo:

See the Pen javascript-common-editor by w3resource (@w3resource) on CodePen.


Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.