0% found this document useful (0 votes)
2 views

Async js challenges

The document outlines a series of async JavaScript challenges categorized into levels ranging from basics to mastery. Each level presents various tasks related to async/await, error handling, microtasks, and real-world use cases, such as implementing debouncing and retry mechanisms. The challenges aim to enhance understanding and proficiency in handling asynchronous operations in JavaScript.

Uploaded by

Luffy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Async js challenges

The document outlines a series of async JavaScript challenges categorized into levels ranging from basics to mastery. Each level presents various tasks related to async/await, error handling, microtasks, and real-world use cases, such as implementing debouncing and retry mechanisms. The challenges aim to enhance understanding and proficiency in handling asynchronous operations in JavaScript.

Uploaded by

Luffy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Async js challenges

09 December 2024 02:30 PM

Level 1: Basics of Async/Await and Event Loop


1. Basic Async Function: Write an async function that uses await with Promise.resolve() and logs
the result.
2. Order of Execution: Explain the output of the following:

javascript
Copy code
async function foo() {
console.log("Start");
const res = await Promise.resolve("Resolved");
console.log(res);
}
foo();
console.log("End");
3. Microtasks and Macrotasks: What will be the output here?

javascript
Copy code
setTimeout(() => console.log("Timeout"), 0);
Promise.resolve().then(() => console.log("Promise"));
console.log("Script");
4. Immediate Promise Resolution: Why does this not print "Inside .then" before "Done"?

javascript
Copy code
console.log("Start");
async function test() {
console.log("Async Function");
return "Done";
}
test().then(() => console.log("Inside .then"));
console.log("End");

Level 2: Nested Promises and Async Functions


5. Nested await: Write an async function that waits for a promise, which itself waits for another
promise. Track the order of execution.
6. Promise.all with Async/Await: Create an async function that fetches data from two APIs
simultaneously using Promise.all and await.
7. Promise Chaining in Async Functions: Convert this code to use async/await:

javascript
Copy code
fetch("https://jsonplaceholder.typicode.com/posts")
.then((res) => res.json())
.then((data) => console.log(data[0].title));
8. Mix of await and .then: Rewrite this using a mix of await and .then. Explain the order of logs:

javascript
Copy code
async function main() {
const res = await fetch("https://jsonplaceholder.typicode.com/posts");
res.json().then((data) => console.log("Title:", data[0].title));

AsyncJS Page 1
res.json().then((data) => console.log("Title:", data[0].title));
console.log("After fetch");
}
main();

Level 3: Handling Errors


9. Error Handling: Add proper error handling to this async function:

javascript
Copy code
async function fetchData() {
const res = await fetch("https://invalid-url");
const data = await res.json();
console.log(data);
}
10. Try-Catch in Nested Async Functions: Write two async functions where one calls the other,
and handle errors at both levels.
11. Promise Rejection: Why does this fail, and how can you fix it?

javascript
Copy code
async function fail() {
return Promise.reject("Error occurred");
}
fail();

Level 4: Combining Microtasks and Macrotasks


12. Microtasks and Async/Await: Predict the output:

javascript
Copy code
async function foo() {
console.log("Async Function Start");
const result = await Promise.resolve("Resolved Value");
console.log(result);
}
console.log("Script Start");
foo();
console.log("Script End");
13. Interleaving Macrotasks: Explain the execution order:

javascript
Copy code
setTimeout(() => console.log("Macrotask 1"), 0);
Promise.resolve().then(() => console.log("Microtask 1"));
setTimeout(() => console.log("Macrotask 2"), 0);
Promise.resolve().then(() => console.log("Microtask 2"));
console.log("Sync Code");
14. Event Loop with Multiple Async Calls: Analyze the order of execution:

javascript
Copy code
async function first() {
console.log("First Function Start");
await second();
console.log("First Function End");
}

AsyncJS Page 2
}
async function second() {
console.log("Second Function");
}
console.log("Global Start");
first();
console.log("Global End");

Level 5: Real-World Use Cases


15. Debouncing with Async Functions: Create a debouncing mechanism using async/await for an
API call.
16. Retry Mechanism: Implement a retry mechanism where a failed API call retries up to 3 times
before throwing an error.
17. Parallel API Calls: Fetch data from 5 APIs in parallel and return the results in an array.
18. Sequential API Calls: Fetch data from 5 APIs sequentially, ensuring each call waits for the
previous one to complete.

Level 6: Advanced Scenarios


19. Throttling API Requests: Fetch data from a list of URLs, but ensure only 3 requests are
processed concurrently.
20. Chaining Async Functions: Create a series of async functions where the output of one function
is the input to the next.
21. Dynamic Promise.all: Dynamically generate 10 promises, each resolving after a random time,
and log when all are resolved.
22. Error Recovery with Async/Await: Implement an async function that retries a failing API call
but falls back to a default value after 3 attempts.
23. Racing Promises: Fetch data from two APIs, and return the result from the one that resolves
first.

Level 7: Complex Challenges


24. Custom Promise-Based Function: Write a function delay(ms) that returns a promise resolved
after ms milliseconds. Use this to create a delay between API calls.
25. Custom Async Queue: Implement an async queue where tasks are processed one by one, even
if added dynamically.
26. Timeout with Fetch: Wrap a fetch call with a timeout function to cancel the request if it takes
too long.
27. Deadlock Simulation: Simulate a scenario where improper use of promises causes a deadlock.
Explain why it happens.
28. Task Scheduler: Write a scheduler that processes tasks at specific intervals using setTimeout
and async/await.

Level 8: Mastery Challenges


29. Recursive Async Calls: Write a recursive async function that fetches data from an API and
stops when a specific condition is met.
30. Data Pipeline: Create a data pipeline where:
○ An async function fetches raw data.
○ Another async function processes the data.
○ A final async function stores the data.

AsyncJS Page 3

You might also like