Practice 50 JavaScript Asynchronous Programming Questions
Q1. Write a JavaScript program to simulate an asynchronous task using setTimeout.
Input: A message "Task completed!" after 2 seconds
Expected Output: The message "Task completed!" logs after 2 seconds.
Q2. Write a JavaScript program to fetch data from an API using fetch() and display it in the console.
Input: A GET request to https://jsonplaceholder.typicode.com/posts/1
Expected Output: The post data is logged in the console.
Q3. Write a JavaScript program to handle multiple asynchronous tasks using Promise.all().
Input: Two promises that resolve with "Task 1 done" and "Task 2 done"
Expected Output: "Task 1 done", "Task 2 done" are logged after both promises resolve.
Q4. Write a JavaScript program to create a promise that resolves after 3 seconds.
Input: resolve("Success!") after 3 seconds
Expected Output: Logs "Success!" after 3 seconds.
Q5. Write a JavaScript program to reject a promise and handle the rejection with catch().
Input: A promise that rejects with "Error!"
Expected Output: Logs "Error!" in the console.
Q6. Write a JavaScript program to use setInterval to log a message every 2 seconds.
Input: Repeated logging of "Hello!" every 2 seconds
Expected Output: "Hello!" logs every 2 seconds.
Q7. Write a JavaScript program to create a delay function using Promise and setTimeout.
Input: Delay of 2 seconds
Expected Output: A message "Delay completed" logs after 2 seconds.
Q8. Write a JavaScript program to fetch data using fetch() with error handling.
Input: GET request to a non-existent URL
Expected Output: Logs an error "Failed to fetch".
Q9. Write a JavaScript program to create a Promise that resolves after 5 seconds and displays "Task completed".
Input: resolve("Task completed") after 5 seconds
Expected Output: "Task completed" logs after 5 seconds.
Q10. Write a JavaScript program to chain promises using .then() and .catch().
Input: Promise that resolves "Step 1 done"
Expected Output: Logs "Step 1 done", then "Step 2 done" after the next .then().
Q11. Write a JavaScript program to handle asynchronous tasks with async/await.
Input: Async function that resolves "Data fetched" after 3 seconds
Expected Output: Logs "Data fetched" after 3 seconds.
Q12. Write a JavaScript program that logs the result of two asynchronous tasks using Promise.all().
Input: Two promises resolving to "Result 1" and "Result 2"
Expected Output: "Result 1", "Result 2" logs after both resolve.
Q13. Write a JavaScript program to simulate a delayed fetch request using setTimeout.
Input: A setTimeout that resolves after 2 seconds
Expected Output: "Data loaded" logs after 2 seconds.
Q14. Write a JavaScript program to fetch user data from an API asynchronously and log the result.
Input: Fetch from https://jsonplaceholder.typicode.com/users/1
Expected Output: Logs the user data.
Q15. Write a JavaScript program to handle a timeout using setTimeout.
Input: A message that logs "Timeout reached!" after 4 seconds
Expected Output: "Timeout reached!" logs after 4 seconds.
Q16. Write a JavaScript program to log a series of messages with a delay between each using setTimeout.
Input: Series of messages "Message 1", "Message 2", "Message 3", each delayed by 1 second
Expected Output: Each message logs with a delay of 1 second.
Q17. Write a JavaScript program to simulate a promise that resolves after a delay and chains two promises.
Input: Resolving "First task done" after 2 seconds, followed by "Second task done" after 3 seconds
Expected Output: "First task done", then "Second task done".
Q18. Write a JavaScript program to create an asynchronous function using async that returns data after 2 seconds.
Input: Async function that returns "Data fetched"
Expected Output: "Data fetched" logs after 2 seconds.
Q19. Write a JavaScript program to demonstrate a promise rejection after 3 seconds.
Input: A promise rejecting with "Error occurred" after 3 seconds
Expected Output: Logs "Error occurred" after 3 seconds.
Q20. Write a JavaScript program to log the result of an asynchronous operation with async/await.
Input: Async function that resolves with "Success"
Expected Output: "Success" logs after the operation resolves.
Q21. Write a JavaScript program to log a message after a delay using Promise and setTimeout.
Input: Promise resolves with "Done!" after 2 seconds
Expected Output: "Done!" logs after 2 seconds.
Q22. Write a JavaScript program to fetch data from an API and handle it asynchronously using async/await.
Input: Fetch request to https://jsonplaceholder.typicode.com/todos/1
Expected Output: Logs the todo data.
Q23. Write a JavaScript program to handle multiple promises using Promise.allSettled().
Input: Two promises that resolve and reject
Expected Output: Logs both results, resolved and rejected, with their statuses.
Q24. Write a JavaScript program to delay the execution of a function using setTimeout.
Input: A function that logs "Delayed!" after 3 seconds
Expected Output: Logs "Delayed!" after 3 seconds.
Q25. Write a JavaScript program to demonstrate Promise.race() with two promises.
Input: Promise that resolves with "Fastest" and another that resolves after 5 seconds
Expected Output: "Fastest" logs before the second promise resolves.
Q26. Write a JavaScript program to make an asynchronous fetch request to retrieve data and log it.
Input: Fetch request to https://jsonplaceholder.typicode.com/posts/1
Expected Output: Logs the post data from the API.
Q27. Write a JavaScript program to simulate an asynchronous data loading with a delay of 4 seconds using async/await.
Input: A delayed function returning "Data loaded"
Expected Output: Logs "Data loaded" after 4 seconds.
Q28. Write a JavaScript program to simulate a task using setInterval and stop it after 5 seconds.
Input: setInterval logging "Running..." every second
Expected Output: Stops after 5 seconds.
Q29. Write a JavaScript program to create an asynchronous function that fetches data with error handling.
Input: Fetch request to a non-existent URL
Expected Output: Logs "Failed to fetch".
Q30. Write a JavaScript program to demonstrate a chained promise that logs "Step 1", "Step 2", and "Step 3".
Input: Chained promises with delays
Expected Output: Logs "Step 1", then "Step 2", then "Step 3".
Q31. Write a JavaScript program to show how to use setTimeout to delay a function.
Input: A function logging "Timeout!" after 2 seconds
Expected Output: Logs "Timeout!" after 2 seconds.
Q32. Write a JavaScript program to fetch a resource from an API using fetch() and log the result with error handling.
Input: Fetch request to https://jsonplaceholder.typicode.com/posts/
Expected Output: Logs the posts or an error message if it fails.
Q33. Write a JavaScript program to demonstrate how to use setInterval to repeat a task every 3 seconds.
Input: Log "Task repeated" every 3 seconds
Expected Output: "Task repeated" logs every 3 seconds.
Q34. Write a JavaScript program to simulate fetching data and displaying it asynchronously after 2 seconds.
Input: Simulate an asynchronous fetch with a setTimeout
Expected Output: Logs "Data fetched" after 2 seconds.
Q35. Write a JavaScript program that fetches data asynchronously and logs each element of an array from the API.
Input: Fetch request to https://jsonplaceholder.typicode.com/posts
Expected Output: Logs each post title from the response.
Q36. Write a JavaScript program to use async/await for fetching data and logging it.
Input: Fetch data from https://jsonplaceholder.typicode.com/posts/1
Expected Output: Logs the fetched post data.
Q37. Write a JavaScript program to use Promise.all to handle multiple asynchronous tasks.
Input: Two promises resolving "Task 1 done" and "Task 2 done"
Expected Output: Both tasks are logged once both are completed.
Q38. Write a JavaScript program to simulate an asynchronous task with setTimeout that logs "Task Complete".
Input: setTimeout function resolving "Task Complete" after 4 seconds
Expected Output: "Task Complete" logs after 4 seconds.
Q39. Write a JavaScript program to simulate a simple API request that resolves and logs "API Data" after 3 seconds.
Input: Simulated API request
Expected Output: Logs "API Data" after 3 seconds.
Q40. Write a JavaScript program to create an async function that delays the execution of logging "Hello!" for 2 seconds.
Input: async/await with a 2-second delay
Expected Output: "Hello!" logs after 2 seconds.
Q41. Write a JavaScript program to use Promise.race() to race between two promises.
Input: One promise resolves fast, another after 5 seconds
Expected Output: Logs the first resolved promise.
Q42. Write a JavaScript program to simulate a delayed response using setTimeout with a promise.
Input: setTimeout wrapped in a promise resolving "Completed"
Expected Output: Logs "Completed" after a delay.
Q43. Write a JavaScript program that simulates a delayed fetch request using async/await.
Input: Simulate fetching data after 3 seconds
Expected Output: Logs "Data fetched" after 3 seconds.
Q44. Write a JavaScript program that fetches data asynchronously and logs the content to the console.
Input: Fetch data from an external API
Expected Output: Logs the fetched data.
Q45. Write a JavaScript program to handle multiple asynchronous tasks with Promise.allSettled().
Input: Two promises that resolve and reject
Expected Output: Logs both resolved and rejected results.
Q46. Write a JavaScript program that fetches data from a JSON file asynchronously.
Input: Fetch request to a local JSON file
Expected Output: Logs the JSON data.
Q47. Write a JavaScript program to chain multiple promises and handle errors with .catch().
Input: Multiple chained promises
Expected Output: Logs success messages or catches and logs errors.
Q48. Write a JavaScript program to display the result of an asynchronous operation using async/await.
Input: Async function that fetches a list of items
Expected Output: Logs the list of items once fetched.
Q49. Write a JavaScript program to simulate loading an image after a delay using promises.
Input: Promise with setTimeout resolving "Image loaded"
Expected Output: Logs "Image loaded" after the delay.
Q50. Write a JavaScript program to simulate a delay in a process and log "Process completed" after 5 seconds.
Input: Simulate a delayed process
Expected Output: Logs "Process completed" after 5 seconds.
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!