- What is asynchronous programming, and why is it important in JavaScript?
- Explain the difference between synchronous and asynchronous code execution.
- What is a callback function? Provide an example.
- What are some of the common problems associated with using callbacks extensively?
- What is a Promise in JavaScript? How does it improve code readability over callbacks?
- Explain the different states of a Promise and what they represent.
- How does the
.then()
method work with Promises? - What is the purpose of the
.catch()
method in Promises? - How does
async/await
simplify working with Promises? - Can
async/await
be used without Promises? Why or why not?
-
Write a function
fetchData(callback)
that simulates fetching data from an API. UsesetTimeout
to delay the execution for 2 seconds. The function should return either data or an error message via the callback. -
Convert the following callback-based function into one that uses Promises:
function getUserData(callback) { setTimeout(() => { callback(null, { name: "John", age: 25 }); }, 1000); }
-
Write a Promise-based function
getTemperature(city)
that resolves with a simulated temperature for the given city after 2 seconds. -
Given the following code snippet, fix it using
.catch()
to handle the error:fetchWeatherData() .then(data => console.log(data)) .then(() => console.log("Done"));
-
Use
async/await
to refactor the following code:function fetchUser() { return new Promise(resolve => { setTimeout(() => resolve({ name: "Alice" }), 1000); }); } fetchUser().then(user => console.log(user));
-
Create an
async
function that fetches user data and weather data sequentially using the following mock functions:function getUser() { return new Promise(resolve => setTimeout(() => resolve("User data"), 1000)); } function getWeather() { return new Promise(resolve => setTimeout(() => resolve("Weather data"), 1000)); }
-
Handle multiple Promises using
Promise.all
to fetch details of three users simultaneously. -
Rewrite the following code to handle errors gracefully using
try...catch
:async function fetchUserData() { let response = await fetch("/user"); let data = await response.json(); console.log(data); }
-
Write an
async
function that throws an error intentionally and handles it usingtry...catch
. -
Create a chain of Promises that simulates a series of dependent asynchronous operations:
- Fetch user profile
- Fetch user settings based on profile
- Fetch recommendations based on settings
-
What happens if you forget to use
await
inside anasync
function? Provide an example. -
Identify the issue with the following code and correct it:
async function fetchData() { setTimeout(() => { return "Data"; }, 1000); } let data = fetchData(); console.log(data);
-
Why does the following code result in a rejected Promise, and how can you fix it?
function fetchItems() { return new Promise((resolve, reject) => { setTimeout(() => reject("Error fetching items"), 2000); }); } fetchItems().then(items => console.log(items));
-
Rewrite the following function to use
Promise.allSettled
:const promise1 = Promise.resolve("Data 1"); const promise2 = Promise.reject("Error in Data 2"); const promise3 = Promise.resolve("Data 3"); Promise.all([promise1, promise2, promise3]).then(results => console.log(results));
-
Consider the following code. Will it work as intended? Explain why or why not:
async function processData() { const data = fetch("/data"); console.log(await data.json()); } processData();
-
Write an
async
function that demonstrates the usage ofPromise.race
with two Promises: one that resolves quickly and another that takes longer. -
What is the difference between
Promise.all
andPromise.race
? Provide an example for each.
- Create a small program that simulates a "Retry Mechanism" using Promises. The function should attempt to fetch data from a mocked API up to 3 times if it encounters an error.