Asynchronous JS Assignment– 3
Basic Questions
- Write an async function that waits 2 seconds using setTimeout and then logs “Hello Async”.
- Create an async function that returns “Resolved Value” and log it using await.
- Write an async function that rejects with “Something went wrong” and handle it with try-catch.
- Implement a function waitAndSquare(n) that waits 1 second and returns n * n.
- Write an async function that calls another async function sequentially.
- Create a function that fetches two numbers asynchronously and returns their sum.
- Write an async function that loops through [1,2,3,4] and logs squares with delay.
- Implement an async function that waits for a random delay (1–3s) before resolving.
- Write a function that throws error if input < 0, otherwise resolves after 1s.
- Create an async timer that prints “Tick 1”, “Tick 2”, “Tick 3” with 1-second gaps.
- Write an async function that simulates fetching a username and returns “Sarthak”.
- Implement an async function that checks if a number is prime and resolves true/false.
- Write an async function that simulates coin toss and resolves “Heads” or “Tails”.
- Create an async function that waits for 3 API calls sequentially and logs result one by one.
- Implement a function that takes an array of numbers and resolves with sum using async-await.
- Write an async function that returns reversed string of given input after 1s delay.
- Create async function getEvenNumbers([1,2,3,4,5]) → should filter even numbers.
- Write an async function to simulate login (user=admin, pass=123) → resolve “Success” else reject “Invalid”.
- Create async function to simulate loading animation with “Loading .” → “Loading ..” → “Loading …” using delays.
- Implement async function that generates 5 random numbers sequentially with 1 second delay.
Intermediate Questions
- Convert a callback-based function into async-await version.
- Write an async function that fetches user data from a fake API and prints “User: Name”.
- Implement a function that uses try-catch-finally where finally always logs “Process Ended”.
- Create async function to fetch posts for a user after fetching user ID (chained calls).
- Implement parallel execution of three async tasks using Promise.all() with async-await.
- Simulate reading file contents using async-await (resolve “File Data” after 1s).
- Write an async function that executes three awaited operations inside a single try-catch.
- Implement function to fetch multiple values and print them in order even if executed in parallel.
- Refactor nested setTimeout (callback hell) into async-await with proper sequence.
- Write async function that processes [“task1”, “task2”, “task3”] sequentially.
- Create async function that delays for 3s before returning “Done” using setTimeout.
- Convert Promise.allSettled example into async-await with resolved/rejected promises.
- Write two async functions (fetchData and processData) and call them sequentially.
- Implement async function that fetches data, transforms string to uppercase, and returns it.
- Demonstrate error propagation where child async function throws and parent catches.
- Create async function to fetch multiple APIs and log the first result using Promise.race.
- Write async function that handles both resolved and rejected Promises in a loop.
- Use async-await with optional chaining to safely access user.profile.email.
- Dynamically import a module inside async function and log something from it.
- Write async function that demonstrates how await pauses execution while other tasks run in event loop.
Advanced Questions
- Implement async function that runs steps “Validate → Process → Save → Respond” sequentially.
- Write async function to fetch paginated API data until page === 5.
- Simulate async database transaction where if one step fails → rollback everything.
- Execute 5 async tasks in parallel but handle errors individually for each task.
- Implement infinite loop with async-await that retries until random number > 8.
- Use async-await to simulate WebSocket messages every 2 seconds.
- Implement retry logic: try API 3 times with 1s gap before failing.
- Write async function that fetches from two fake databases (Mongo + SQL) in parallel.
- Create file processing pipeline: “Read → Transform → Save” with async-await and error handling.
- Optimize batch execution: process array of 20 items, but only 5 run concurrently at a time.