Asynchronous JS Assignment– 3

Basic Questions

  1. Write an async function that waits 2 seconds using setTimeout and then logs “Hello Async”.
  2. Create an async function that returns “Resolved Value” and log it using await.
  3. Write an async function that rejects with “Something went wrong” and handle it with try-catch.
  4. Implement a function waitAndSquare(n) that waits 1 second and returns n * n.
  5. Write an async function that calls another async function sequentially.
  6. Create a function that fetches two numbers asynchronously and returns their sum.
  7. Write an async function that loops through [1,2,3,4] and logs squares with delay.
  8. Implement an async function that waits for a random delay (1–3s) before resolving.
  9. Write a function that throws error if input < 0, otherwise resolves after 1s.
  10. Create an async timer that prints “Tick 1”, “Tick 2”, “Tick 3” with 1-second gaps.
  11. Write an async function that simulates fetching a username and returns “Sarthak”.
  12. Implement an async function that checks if a number is prime and resolves true/false.
  13. Write an async function that simulates coin toss and resolves “Heads” or “Tails”.
  14. Create an async function that waits for 3 API calls sequentially and logs result one by one.
  15. Implement a function that takes an array of numbers and resolves with sum using async-await.
  16. Write an async function that returns reversed string of given input after 1s delay.
  17. Create async function getEvenNumbers([1,2,3,4,5]) → should filter even numbers.
  18. Write an async function to simulate login (user=admin, pass=123) → resolve “Success” else reject “Invalid”.
  19. Create async function to simulate loading animation with “Loading .” → “Loading ..” → “Loading …” using delays.
  20. Implement async function that generates 5 random numbers sequentially with 1 second delay.

Intermediate Questions

  1. Convert a callback-based function into async-await version.
  2. Write an async function that fetches user data from a fake API and prints “User: Name”.
  3. Implement a function that uses try-catch-finally where finally always logs “Process Ended”.
  4. Create async function to fetch posts for a user after fetching user ID (chained calls).
  5. Implement parallel execution of three async tasks using Promise.all() with async-await.
  6. Simulate reading file contents using async-await (resolve “File Data” after 1s).
  7. Write an async function that executes three awaited operations inside a single try-catch.
  8. Implement function to fetch multiple values and print them in order even if executed in parallel.
  9. Refactor nested setTimeout (callback hell) into async-await with proper sequence.
  10. Write async function that processes [“task1”, “task2”, “task3”] sequentially.
  11. Create async function that delays for 3s before returning “Done” using setTimeout.
  12. Convert Promise.allSettled example into async-await with resolved/rejected promises.
  13. Write two async functions (fetchData and processData) and call them sequentially.
  14. Implement async function that fetches data, transforms string to uppercase, and returns it.
  15. Demonstrate error propagation where child async function throws and parent catches.
  16. Create async function to fetch multiple APIs and log the first result using Promise.race.
  17. Write async function that handles both resolved and rejected Promises in a loop.
  18. Use async-await with optional chaining to safely access user.profile.email.
  19. Dynamically import a module inside async function and log something from it.
  20. Write async function that demonstrates how await pauses execution while other tasks run in event loop.

Advanced Questions

  1. Implement async function that runs steps “Validate → Process → Save → Respond” sequentially.
  2. Write async function to fetch paginated API data until page === 5.
  3. Simulate async database transaction where if one step fails → rollback everything.
  4. Execute 5 async tasks in parallel but handle errors individually for each task.
  5. Implement infinite loop with async-await that retries until random number > 8.
  6. Use async-await to simulate WebSocket messages every 2 seconds.
  7. Implement retry logic: try API 3 times with 1s gap before failing.
  8. Write async function that fetches from two fake databases (Mongo + SQL) in parallel.
  9. Create file processing pipeline: “Read → Transform → Save” with async-await and error handling.
  10. Optimize batch execution: process array of 20 items, but only 5 run concurrently at a time.