Asynchronous JS Assignment– 5
Basic Questions
- Write a function that runs two async tasks concurrently and logs when both finish.
- Write code to compare parallel execution vs sequential execution of two promises and log the time difference.
- Use Promise.all to fetch two APIs simultaneously and log both results.
- Create three promises (resolve after 1s, 2s, 3s) and use Promise.all to log results.
- Create an async iterator that yields 5 random numbers with a delay of 1 second each.
- Implement both a normal iterator and an async iterator that generate numbers, then log their outputs.
- Use for-await-of to iterate through an async generator that returns numbers 1–5 with delays.
- Simulate a Server-Sent Events (SSE) endpoint with EventSource in the browser and log messages.
- Subscribe to a fake SSE endpoint and log data as it arrives.
- Write a fetch request to consume a server stream and log each chunk of text received.
- Use fetch to request a large text file and log the chunks as they stream.
- Create a fetch request that reads response body as a stream and logs first 50 characters.
- Convert a ReadableStream into full text and log it.
- Write a function that queries a fake DB asynchronously and returns a user record after 1s.
- Simulate a database fetch with promises and log the retrieved “users” array.
- Write an async function that throws an error randomly and handle it using .catch().
- Demonstrate async-await with try-catch by fetching data from an API.
- Write blocking code (with while loop) and non-blocking async code, and compare their logs.
- Simulate 100 async DB queries and log how async improves response time.
- Call an external API asynchronously and log first 5 results.
Intermediate Questions
- Implement three async functions (task1, task2, task3) that run sequentially with delays.
- Implement the same three async functions but run them in parallel with Promise.all.
- Fetch data from three APIs in parallel and merge results into one array.
- Simulate an async database cursor using an async generator and iterate with for-await-of.
- Write a Promise.all example where one promise rejects, and handle the error gracefully.
- Fetch paginated data (5 pages) from an API using async-await and for-await-of.
- Create a stream that produces numbers quickly and implement backpressure handling with reader.read().
- Use fetch streaming to read a large JSON chunk by chunk and parse it progressively.
- Fetch a text file and convert its response stream into a Blob object.
- Write Node.js code to consume SSE from a server and log events.
- Use EventSource in browser JS to listen to “message” events and log payloads.
- Simulate 3 async DB queries with Promise.allSettled and handle errors properly.
- Create a small microservice simulation where 2 async services are called and merged into one response.
- Fetch posts and their comments in parallel, then merge each post with its comments.
- Write async code that intentionally causes callback hell, then refactor using async-await.
- Use console.time and console.timeEnd to measure async function execution time.
- Write async code with AbortController that cancels a fetch request after 2 seconds.
- Compare fetch with and without streaming for a large dataset, and log memory usage.
- Implement retry logic for a fetch that fails randomly (retry max 3 times).
- Write two async DB operations running in parallel and ensure final result is consistent using locks.
Advanced Questions
- Create a microservice function that queries two mock databases asynchronously and merges users + orders.
- Consume an SSE stream that sends data every 2s, and auto-reconnect if connection breaks.
- Use fetch streaming to download a 1MB file in chunks and log progress percentage.
- Run two sets of async tasks sequentially vs in parallel and log the total execution time for comparison.
- Write async code that updates a shared counter in parallel and demonstrate a race condition.
- Use async iterators to process 100,000 fake DB rows in batches of 100 efficiently.
- Use async-await + AbortController to cancel multiple fetch requests after 3s timeout.
- Implement exponential backoff retry (1s → 2s → 4s → 8s) for failed API calls.
- Write async code with wrong await placement that causes a deadlock, then fix it.
- Create a logging utility that tracks async function start, end, and execution time across multiple services.