Asynchronous JS Assignment– 5

Basic Questions

  1. Write a function that runs two async tasks concurrently and logs when both finish.
  2. Write code to compare parallel execution vs sequential execution of two promises and log the time difference.
  3. Use Promise.all to fetch two APIs simultaneously and log both results.
  4. Create three promises (resolve after 1s, 2s, 3s) and use Promise.all to log results.
  5. Create an async iterator that yields 5 random numbers with a delay of 1 second each.
  6. Implement both a normal iterator and an async iterator that generate numbers, then log their outputs.
  7. Use for-await-of to iterate through an async generator that returns numbers 1–5 with delays.
  8. Simulate a Server-Sent Events (SSE) endpoint with EventSource in the browser and log messages.
  9. Subscribe to a fake SSE endpoint and log data as it arrives.
  10. Write a fetch request to consume a server stream and log each chunk of text received.
  11. Use fetch to request a large text file and log the chunks as they stream.
  12. Create a fetch request that reads response body as a stream and logs first 50 characters.
  13. Convert a ReadableStream into full text and log it.
  14. Write a function that queries a fake DB asynchronously and returns a user record after 1s.
  15. Simulate a database fetch with promises and log the retrieved “users” array.
  16. Write an async function that throws an error randomly and handle it using .catch().
  17. Demonstrate async-await with try-catch by fetching data from an API.
  18. Write blocking code (with while loop) and non-blocking async code, and compare their logs.
  19. Simulate 100 async DB queries and log how async improves response time.
  20. Call an external API asynchronously and log first 5 results.

Intermediate Questions

  1. Implement three async functions (task1, task2, task3) that run sequentially with delays.
  2. Implement the same three async functions but run them in parallel with Promise.all.
  3. Fetch data from three APIs in parallel and merge results into one array.
  4. Simulate an async database cursor using an async generator and iterate with for-await-of.
  5. Write a Promise.all example where one promise rejects, and handle the error gracefully.
  6. Fetch paginated data (5 pages) from an API using async-await and for-await-of.
  7. Create a stream that produces numbers quickly and implement backpressure handling with reader.read().
  8. Use fetch streaming to read a large JSON chunk by chunk and parse it progressively.
  9. Fetch a text file and convert its response stream into a Blob object.
  10. Write Node.js code to consume SSE from a server and log events.
  11. Use EventSource in browser JS to listen to “message” events and log payloads.
  12. Simulate 3 async DB queries with Promise.allSettled and handle errors properly.
  13. Create a small microservice simulation where 2 async services are called and merged into one response.
  14. Fetch posts and their comments in parallel, then merge each post with its comments.
  15. Write async code that intentionally causes callback hell, then refactor using async-await.
  16. Use console.time and console.timeEnd to measure async function execution time.
  17. Write async code with AbortController that cancels a fetch request after 2 seconds.
  18. Compare fetch with and without streaming for a large dataset, and log memory usage.
  19. Implement retry logic for a fetch that fails randomly (retry max 3 times).
  20. Write two async DB operations running in parallel and ensure final result is consistent using locks.

Advanced Questions

  1. Create a microservice function that queries two mock databases asynchronously and merges users + orders.
  2. Consume an SSE stream that sends data every 2s, and auto-reconnect if connection breaks.
  3. Use fetch streaming to download a 1MB file in chunks and log progress percentage.
  4. Run two sets of async tasks sequentially vs in parallel and log the total execution time for comparison.
  5. Write async code that updates a shared counter in parallel and demonstrate a race condition.
  6. Use async iterators to process 100,000 fake DB rows in batches of 100 efficiently.
  7. Use async-await + AbortController to cancel multiple fetch requests after 3s timeout.
  8. Implement exponential backoff retry (1s → 2s → 4s → 8s) for failed API calls.
  9. Write async code with wrong await placement that causes a deadlock, then fix it.
  10. Create a logging utility that tracks async function start, end, and execution time across multiple services.