In this chapter, we dive into the powerful features of JavaScript—Async/Await and advanced promises. We explore how to simplify asynchronous code, handle errors effectively, and manage parallel and sequential promises using Promise.all and Promise.race.
MCQs on Async/Await and Advanced Promises
Introduction to Async and Await
What is the primary benefit of using async and await in JavaScript?
A) Makes asynchronous code run synchronously
B) Simplifies asynchronous code and makes it easier to read
C) Reduces memory consumption
D) Only works with synchronous functions
What keyword is used to define an asynchronous function in JavaScript?
A) async
B) await
C) promise
D) asyncFunc
What does await do inside an asynchronous function?
A) Pauses the execution of the entire program
B) Waits for the promise to resolve and returns the result
C) Immediately returns the promise object
D) Cancels the promise if it takes too long
What type of value can an async function return?
A) Only a promise
B) Any value, wrapped in a promise
C) Only a string
D) Undefined
Which of the following will throw an error when used with await?
A) A resolved promise
B) A pending promise
C) A non-promise value
D) An undefined value
Error Handling with try…catch in Async Functions
How do you handle errors in an asynchronous function?
A) Using try…catch block
B) Using promises directly
C) By catching them with .catch()
D) By using a callback function
Which of the following is correct when using try...catch in async functions?
A) catch block can only handle sync errors
B) try block catches both sync and async errors
C) try...catch only works in synchronous functions
D) You don’t need a catch block
What happens when an error occurs inside an async function but there is no catch block?
A) The error is automatically logged to the console
B) The program terminates immediately
C) The promise is rejected with the error
D) The promise is ignored and resolved with null
In which scenario should you use try...catch inside an async function?
A) To handle errors synchronously
B) To handle errors from awaited promises
C) To handle JavaScript syntax errors
D) To debug your code
What is the behavior when an exception is thrown in an async function without a try...catch?
A) The promise will resolve to undefined
B) The promise will resolve to the exception message
C) The promise will be rejected with the exception
D) The exception is ignored and the code continues
Promise.all, Promise.race
What is the purpose of Promise.all in JavaScript?
A) To execute multiple promises sequentially
B) To wait for all promises to resolve and return the results
C) To handle errors in a promise
D) To cancel a promise
Which of the following is true when using Promise.all?
A) It returns a promise that resolves when any of the promises resolve
B) It rejects immediately if any of the promises reject
C) It waits for the first promise to reject
D) It only resolves when the last promise resolves
What does Promise.race do?
A) Resolves as soon as one of the promises resolves or rejects
B) Resolves after all promises have resolved
C) Always rejects if any promise rejects
D) Resolves only if all promises resolve
When using Promise.all, what happens if one of the promises rejects?
A) It rejects all other promises immediately
B) It resolves with the remaining promises
C) It continues to wait for other promises to resolve
D) It ignores the rejection
In Promise.race, which promise’s result is returned?
A) The last promise to resolve
B) The first promise to resolve or reject
C) The promise with the lowest priority
D) All promises’ results are returned
Which of the following is not supported by Promise.all?
A) Resolving multiple promises
B) Returning results as an array
C) Rejecting when one promise fails
D) Returning a promise with a single value
How can Promise.race be useful in handling timeouts?
A) It waits for a specific promise to reject first
B) It resolves the first promise to finish, including timeouts
C) It waits for a timeout to occur before resolving any promise
D) It cancels the other promises once one is completed
If you want to execute multiple promises in parallel but need to resolve when all are completed, which method should you use?
A) Promise.all
B) Promise.race
C) Promise.any
D) setTimeout
What does Promise.all return if any of the promises reject?
A) The rejection reason
B) The resolved value from all promises
C) An array of the results
D) A resolved promise with null values
How would you use Promise.all to run asynchronous functions in parallel?
A) By passing an array of promises to Promise.all
B) By passing a single promise object to Promise.all
C) By chaining promises together
D) By using await with each promise sequentially
Parallel and Sequential Execution of Promises
Which of the following will execute promises sequentially in JavaScript?
A) Using Promise.all
B) Using Promise.race
C) Using await inside a loop
D) Using setTimeout
How do you execute promises in parallel and wait for all of them to complete?
A) Use Promise.all
B) Use Promise.race
C) Use Promise.resolve()
D) Use forEach
Which of the following will run promises sequentially, one after the other?
A) Promise.all
B) Promise.race
C) A for loop with await
D) Promise.any
What is the advantage of running promises sequentially rather than in parallel?
A) Better for handling time-sensitive tasks
B) Allows handling each promise one by one in a specific order
C) Faster execution time
D) More efficient memory usage
When using async/await inside a loop, how can you run promises sequentially?
A) By using Promise.all inside the loop
B) By using await inside the loop
C) By chaining promises together
D) By using setTimeout with promises
What happens when you use await inside a loop for sequential execution?
A) All promises will execute in parallel
B) Each promise will wait for the previous one to resolve
C) The program will exit immediately
D) The promises will be ignored
What is the advantage of running promises in parallel?
A) Reduces execution time by executing all promises simultaneously
B) Makes sure promises execute in a specific order
C) Reduces memory usage
D) Guarantees error-free execution
Which of the following is a key difference between parallel and sequential execution of promises?
A) Parallel execution speeds up the process
B) Sequential execution waits for the completion of each promise
C) Parallel execution uses more memory
D) Sequential execution does not involve promises
How can you handle errors when running promises in parallel using Promise.all?
A) By using .catch() on individual promises
B) By using try...catch inside each promise
C) By wrapping Promise.all with try...catch
D) By using setTimeout
What will happen if a rejected promise is in a parallel execution using Promise.all?
A) It will be ignored and the other promises will continue
B) The entire Promise.all will reject immediately
C) The other promises will be delayed
D) It will result in an empty array of results
Answer Key
Qno
Answer (Option with Text)
1
B) Simplifies asynchronous code and makes it easier to read
2
A) async
3
B) Waits for the promise to resolve and returns the result
4
B) Any value, wrapped in a promise
5
C) A non-promise value
6
A) Using try…catch block
7
B) try block catches both sync and async errors
8
C) The promise is rejected with the error
9
B) To handle errors from awaited promises
10
C) The promise will be rejected with the exception
11
B) To wait for all promises to resolve and return the results
12
B) It rejects immediately if any of the promises reject
13
A) Resolves as soon as one of the promises resolves or rejects
14
A) It rejects all other promises immediately
15
B) The first promise to resolve or reject
16
D) Returning a promise with a single value
17
B) It resolves the first promise to finish, including timeouts
18
A) Promise.all
19
A) The rejection reason
20
A) By passing an array of promises to Promise.all
21
C) Using await inside a loop
22
A) Use Promise.all
23
C) A for loop with await
24
B) Allows handling each promise one by one in a specific order
25
B) By using await inside the loop
26
B) Each promise will wait for the previous one to resolve
27
A) Reduces execution time by executing all promises simultaneously
28
B) Sequential execution waits for the completion of each promise