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.
Promise Chain Example:
2. Write a JavaScript function that uses a chain of .then() calls to perform a series of asynchronous tasks.
Handling Promise Rejection:
3. Write a JavaScript program that demonstrates how to catch and handle errors in Promises using .catch().
Promise.any() Example:
4. Write a JavaScript function that takes multiple Promises and resolves with the first successful result using Promise.any().
Promise.allSettled() Example:
5. Write a JavaScript program that takes an array of Promises and logs both resolved and rejected results using Promise.allSettled().
Simulating a Task Queue:
6. Write a JavaScript program that processes an array of asynchronous tasks sequentially using Promises.
Chained Async/Await Example:
7. Write a JavaScript program that uses async/await to perform three asynchronous tasks in sequence.
Error Handling in Async/Await:
8. Write a JavaScript program to demonstrate how to handle errors in async/await functions using try...catch.
Simulating a Throttled API:
9. Write a JavaScript function that ensures only a specified number of asynchronous requests are made simultaneously.
Custom Promise Wrapper:
10. Write a JavaScript function that wraps an asynchronous operation in a Promise.
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.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics