Asynchronous JS Assignment– 2
Basic Questions
- Create a Promise that resolves with “Hello World” after 1 second and log it.
- Write a Promise that rejects after 2 seconds with “Error: Timeout” and handle it.
- Implement a function delay(ms) that returns a Promise resolving after ms milliseconds.
- Write a function that returns a Promise to add two numbers asynchronously.
- Create a Promise that randomly resolves “Success” or rejects “Failed”.
- Write a Promise that simulates checking if a user is logged in (resolve true/false).
- Implement a Promise-based function that multiplies two numbers and logs result.
- Create a Promise that waits 2 seconds and then resolves with a random number.
- Write a function getEvenNumber(n) that returns a Promise which resolves if n is even, rejects otherwise.
- Create a Promise-based timer that counts from 1 to 5 with 1-second delay each step.
- Implement a Promise that resolves to “Data Loaded” after simulating an API delay.
- Create a Promise that rejects if input is not a string, otherwise resolves “Valid String”.
- Implement a Promise that resolves an array [1,2,3,4,5] after 1 second.
- Write a Promise-based function to check if a number is prime.
- Create a Promise that resolves “Task Completed” after 3 seconds and then log it.
- Write a Promise that rejects if given number is negative.
- Implement a Promise-based function getSquare(n) that resolves with square of n.
- Create a Promise chain that prints “First → Second → Third”.
- Write a Promise that simulates validating a password: resolve if “1234”, reject otherwise.
- Implement a Promise that simulates coin toss (resolve “Heads”, reject “Tails”).
Intermediate Questions
- Chain two Promises where first resolves “Step 1 done”, then second resolves “Step 2 done”.
- Create a Promise that rejects with “Network Error” and handle it with .catch().
- Implement a Promise with .finally() that logs “Process Finished” no matter success or failure.
- Simulate fetching user details ({id:1, name:”Sarthak”}) with a Promise.
- Chain three Promises to simulate: “Login → Fetch Data → Show Dashboard”.
- Write a function that uses Promises to fetch two numbers sequentially and return their sum.
- Use Promise.all() to fetch [“user”, “posts”, “comments”] together and log them.
- Simulate two race Promises (fast API and slow API) using Promise.race().
- Use Promise.allSettled() with one resolve and one reject, and log the results.
- Implement Promise.any() with three tasks where only the fastest success is used.
- Write a Promise that rejects after 1 second and handle it with .catch().
- Chain Promises where the second .then() intentionally throws error, handle it in .catch().
- Inside .then(), return a new Promise that resolves after 1 second.
- Create a function that generates random number and resolves if > 5, else rejects.
- Implement nested Promises where the second depends on the first.
- Demonstrate difference of Promise.all() and Promise.race() with code.
- Simulate API call with a Promise that resolves “User Fetched” after 2 seconds.
- Create a function asyncTask() that returns a Promise, call it and handle result outside.
- Write a Promise without .catch() and observe unhandled rejection behavior.
- Refactor a callback hell example (login → getData → showData) into Promises.
Advanced Questions
- Simulate a backend pipeline with Promises: “Validate Input → Process Data → Save to DB → Send Response”.
- Use Promise.allSettled() with 5 Promises (some resolve, some reject) and log status of each.
- Write a function runAny(tasks) that returns Promise.any() result of multiple async tasks.
- Create a chain of Promises where error occurs in the middle and is caught properly.
- Implement a Promise withTimeout(promise, ms) that rejects if promise takes too long.
- Simulate fetching 3 APIs concurrently with Promise.all() and then merge results into one object.
- Demonstrate error bubbling through multiple .then() calls until .catch() handles it.
- Take a callback-based file reader simulation and refactor into Promises.
- Combine Promise.race() and Promise.all() to fetch from multiple APIs with fallback.
- Simulate a backend flow using Promises: “Read File → Transform Data → Save Result →