Asynchronous JS Assignment– 2

Basic Questions

  1. Create a Promise that resolves with “Hello World” after 1 second and log it.
  2. Write a Promise that rejects after 2 seconds with “Error: Timeout” and handle it.
  3. Implement a function delay(ms) that returns a Promise resolving after ms milliseconds.
  4. Write a function that returns a Promise to add two numbers asynchronously.
  5. Create a Promise that randomly resolves “Success” or rejects “Failed”.
  6. Write a Promise that simulates checking if a user is logged in (resolve true/false).
  7. Implement a Promise-based function that multiplies two numbers and logs result.
  8. Create a Promise that waits 2 seconds and then resolves with a random number.
  9. Write a function getEvenNumber(n) that returns a Promise which resolves if n is even, rejects otherwise.
  10. Create a Promise-based timer that counts from 1 to 5 with 1-second delay each step.
  11. Implement a Promise that resolves to “Data Loaded” after simulating an API delay.
  12. Create a Promise that rejects if input is not a string, otherwise resolves “Valid String”.
  13. Implement a Promise that resolves an array [1,2,3,4,5] after 1 second.
  14. Write a Promise-based function to check if a number is prime.
  15. Create a Promise that resolves “Task Completed” after 3 seconds and then log it.
  16. Write a Promise that rejects if given number is negative.
  17. Implement a Promise-based function getSquare(n) that resolves with square of n.
  18. Create a Promise chain that prints “First → Second → Third”.
  19. Write a Promise that simulates validating a password: resolve if “1234”, reject otherwise.
  20. Implement a Promise that simulates coin toss (resolve “Heads”, reject “Tails”).

Intermediate Questions

  1. Chain two Promises where first resolves “Step 1 done”, then second resolves “Step 2 done”.
  2. Create a Promise that rejects with “Network Error” and handle it with .catch().
  3. Implement a Promise with .finally() that logs “Process Finished” no matter success or failure.
  4. Simulate fetching user details ({id:1, name:”Sarthak”}) with a Promise.
  5. Chain three Promises to simulate: “Login → Fetch Data → Show Dashboard”.
  6. Write a function that uses Promises to fetch two numbers sequentially and return their sum.
  7. Use Promise.all() to fetch [“user”, “posts”, “comments”] together and log them.
  8. Simulate two race Promises (fast API and slow API) using Promise.race().
  9. Use Promise.allSettled() with one resolve and one reject, and log the results.
  10. Implement Promise.any() with three tasks where only the fastest success is used.
  11. Write a Promise that rejects after 1 second and handle it with .catch().
  12. Chain Promises where the second .then() intentionally throws error, handle it in .catch().
  13. Inside .then(), return a new Promise that resolves after 1 second.
  14. Create a function that generates random number and resolves if > 5, else rejects.
  15. Implement nested Promises where the second depends on the first.
  16. Demonstrate difference of Promise.all() and Promise.race() with code.
  17. Simulate API call with a Promise that resolves “User Fetched” after 2 seconds.
  18. Create a function asyncTask() that returns a Promise, call it and handle result outside.
  19. Write a Promise without .catch() and observe unhandled rejection behavior.
  20. Refactor a callback hell example (login → getData → showData) into Promises.

Advanced Questions

  1. Simulate a backend pipeline with Promises: “Validate Input → Process Data → Save to DB → Send Response”.
  2. Use Promise.allSettled() with 5 Promises (some resolve, some reject) and log status of each.
  3. Write a function runAny(tasks) that returns Promise.any() result of multiple async tasks.
  4. Create a chain of Promises where error occurs in the middle and is caught properly.
  5. Implement a Promise withTimeout(promise, ms) that rejects if promise takes too long.
  6. Simulate fetching 3 APIs concurrently with Promise.all() and then merge results into one object.
  7. Demonstrate error bubbling through multiple .then() calls until .catch() handles it.
  8. Take a callback-based file reader simulation and refactor into Promises.
  9. Combine Promise.race() and Promise.all() to fetch from multiple APIs with fallback.
  10. Simulate a backend flow using Promises: “Read File → Transform Data → Save Result →