Asynchronous JS Assignment– 1

Basic Questions

  1. Write a JavaScript program to print “Start”, then use setTimeout to print “Hello after 2 seconds”, and then print “End”.
  2. Create a function that uses setTimeout to simulate a 3-second delay before logging “Data fetched”.
  3. Implement a callback function that takes two numbers and prints their sum after 1 second.
  4. Write a function that prints “Task 1” immediately and “Task 2” after 2 seconds.
  5. Use setInterval to print “Running…” every second for 5 seconds and then stop it.
  6. Create a function fetchUser(callback) that returns a mock user object {name: “Sarthak”} after 2 seconds using a callback.
  7. Implement an error-first callback function that either returns “Success” or an error “Something went wrong”.
  8. Write a program that prints “A”, “B”, “C” in order, but “C” should be printed using setTimeout.
  9. Implement a function that delays execution of another function by 1 second.
  10. Write a program to simulate downloading a file using a callback after 3 seconds.
  11. Create a function that prints numbers from 1 to 5 with a 1-second delay between each print.
  12. Implement nested callbacks to print “Step 1”, “Step 2”, “Step 3” with increasing delays.
  13. Write a program to simulate login using a callback, where if username matches “admin”, it prints “Login Success”, else “Login Failed”.
  14. Use setTimeout to print “Ping” every 2 seconds, but stop after 3 times.
  15. Implement a callback hell example where you print “First”, then “Second”, then “Third”, each after 1 second.
  16. Create a function that accepts a callback and executes it after 500ms delay.
  17. Write a program where you simulate fetching 2 users, one after another, using callbacks.
  18. Implement a timer function that counts down from 5 to 0 with 1-second delay.
  19. Write a function that accepts a name and prints “Hello <name>” after 2 seconds using a callback.
  20. Implement a simple retry mechanism where a function runs again if the first attempt fails (simulate with a random error).

Intermediate Questions

  1. Implement a function that simulates an API call with success or failure using error-first callbacks.
  2. Write a program to print “Start”, then schedule “Task A” after 2 seconds, “Task B” after 1 second, and ensure “End” is printed immediately.
  3. Create a function that simulates reading a file with setTimeout and passes the file content to a callback.
  4. Implement a function doTask(taskName, delay, callback) that executes multiple tasks in sequence using callbacks.
  5. Write a program that demonstrates callback hell by simulating multiple API calls: login → fetch profile → fetch posts → show posts.
  6. Implement a function that simulates downloading multiple files sequentially using callbacks.
  7. Write a program where you simulate fetching weather data for 3 cities one after another using nested callbacks.
  8. Create a function that accepts two callbacks: one for success and one for error, and randomly executes one of them.
  9. Write a program that simulates sending an OTP and then verifying it using callbacks.
  10. Implement a function that simulates order processing steps: “Order Placed” → “Packed” → “Shipped” → “Delivered” using callbacks.
  11. Write a function that uses multiple nested setTimeout calls to print numbers 1–10 with a 1-second delay between them.
  12. Create a function that simulates fetching student marks and then calculates the grade using callbacks.
  13. Implement a program that simulates checking a user’s subscription status using a callback with success/failure messages.
  14. Write a program that demonstrates the non-blocking nature of JavaScript by printing async and sync messages together.
  15. Simulate a bank transaction: “Validating User” → “Checking Balance” → “Transaction Successful” using callbacks.
  16. Implement a callback-based calculator supporting add, subtract, multiply, and divide.
  17. Write a program that simulates chaining asynchronous tasks with different delays (e.g., fetch → process → save).
  18. Implement a function that calls three APIs one after another, where each API returns data after 1 second.
  19. Create a program that simulates a chatbot that asks 3 questions in sequence using callbacks.
  20. Write a program that demonstrates the issue of callback hell and then refactor it slightly to make it cleaner using named functions.

Advanced Questions

  1. Simulate an e-commerce checkout process using callbacks: addToCart → applyCoupon → calculateTotal → makePayment → sendReceipt.
  2. Implement a file uploader simulation where multiple files are uploaded sequentially with callbacks.
  3. Write a program that simulates a multi-step user registration: validateEmail → sendOTP → verifyOTP → createAccount using callbacks.
  4. Implement a simulation of an online food delivery process: selectFood → placeOrder → cookFood → deliverFood using callbacks.
  5. Write a callback hell example of fetching data from 3 APIs and then combining the results.
  6. Simulate a movie booking system with callbacks: searchMovie → selectSeats → makePayment → confirmTicket.
  7. Create a callback-based task scheduler that executes tasks at different times.
  8. Write a function that simulates retrying API calls up to 3 times if the first ones fail using callbacks.
  9. Implement a simulation of IoT devices sending data asynchronously with callbacks.
  10. Simulate a social media workflow with callbacks: login → fetchFriends → fetchPosts → likePost.