
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
(Full Stack JavaScript) Async & Await Knowledge Assessment Guide 2024(Full Stack JavaScript) Async & Await Knowledge Assessment Guide 2024(Full Stack JavaScript) Async & Await Knowledge Assessment Guide 2024(Full Stack JavaScript) Async & Await Knowledge Assessment Guide 2024
Typology: Exams
1 / 24
This page cannot be seen from the preview
Don't miss anything!

















await keyword do in an async function? await keyword pauses the execution of the async function until the Promise is resolved or rejected.Promise.all()Promise.race()Promise.allSettled()await keyword can only be used inside an __________ function. await keyword is designed to work only within async functions to pause execution until the Promise is resolved.Promise.all() method returns a single Promise that resolves when __________ of the promises in the iterable have resolved. Promise.all() waits for all Promises in the iterable to resolve before resolving itself.try...catch blocks are used to handle errors in async functions.Promise.race() method returns a Promise that resolves or rejects as soon as __________ of the promises in the iterable resolves or rejects. Promise.race() resolves or rejects as soon as one of the Promises in the iterable resolves or rejects.async keyword in JavaScript? async keyword is used to declare a function that returns a Promise.Promise.allSettled()? Promise.allSettled() resolves after all Promises have settled, regardless of whether they were resolved or rejected.async function test() { throw new Error("Test error"); test().catch(e => console.log(e.message));
- A) Test error - B) undefined - C) Error - D) Promise {<rejected>: Error: Test error} - Answer: A) Test error - Rationale: The async function throws an error, which is caught and logged by the `catch` method. 19. Which method can be used to handle the first resolved Promise among multiple Promises? - A) `Promise.all()` - B) `Promise.race()` - C) `Promise.allSettled()` - D) `Promise.any()` - Answer: B) `Promise.race()` - Rationale: `Promise.race()` resolves or rejects as soon as one of the Promises resolves or rejects. 20. What will be the output of the following code? ```javascript Rationale: The async keyword is used to declare that a function is asynchronous in JavaScript. Fill-in-the-Blank: Async functions always return a __. ## Correct Answer: Promise Rationale: Async functions return a Promise, which represents a value that may be available now, or in the future, or never. True/False: The await keyword can only be used inside an asynchronous function. ## Correct Answer: True Rationale: The await keyword can only be used inside functions declared with the async keyword to wait for a Promise to be resolved. Multiple Choice: Which of the following is not a valid use case for async/await in JavaScript? A. Making AJAX requests B. Performing CPU-intensive tasks C. Handling timeouts D. Animating elements on a webpage ## Correct Answer: D. Animating elements on a webpage Rationale: Async/await is not suitable for handling animations as it is primarily used for managing asynchronous operations. Fill-in-the-Blank: The await keyword is used to wait for a __ to be resolved. ## Correct Answer: Promise Rationale: await pauses the execution of an async function until the Promise is settled. True/False: Async functions always execute synchronously. ## Correct Answer: False Rationale: Async functions allow asynchronous operations to be performed in a synchronous manner, making code easier to read and maintain. Multiple Choice: Which of the following keywords can be used with async/await to handle errors? Multiple Choice: What does the Promise.all method do in JavaScript? A. Executes a group of Promises in parallel B. Executes a group of Promises sequentially C. Executes only the first resolved Promise D. None of the above ## Correct Answer: A. Executes a group of Promises in parallel Rationale: Promise.all takes an iterable of Promises and returns a single Promise that resolves when all the input Promises have resolved. Fill-in-the-Blank: The async keyword can be used with __ to create a Promise-based API. ## Correct Answer: Functions Rationale: By using the async keyword with functions, developers can create asynchronous functions that return Promises. True/False: Using async/await can help avoid callback hell in JavaScript. ## Correct Answer: True Rationale: async/await simplifies asynchronous code and makes it more readable compared to nested callbacks. Multiple Choice: Which of the following is not a state of a Promise in JavaScript? A. Pending B. Resolved C. Rejected D. Completed ## Correct Answer: D. Completed Rationale: Promises in JavaScript have states of Pending, Resolved (Fulfilled), and Rejected. Fill-in-the-Blank: The Promise.race method returns a Promise that resolves or rejects as soon as one of the input Promises __. ## Correct Answer: Settles Rationale: Promise.race returns a Promise that settles (resolves or rejects) as soon as one of the input Promises settles. True/False: Rationale: Async functions can return values using the return keyword, which resolves the Promise with the specified value. True/False: The async keyword can be used with arrow functions in JavaScript. ## Correct Answer: True Rationale: Arrow functions can be declared as async functions by prefixing them with the async keyword. Multiple Choice: Which of the following is a benefit of using async/await over Promises in JavaScript? A. Better performance B. Simpler error handling C. Easier debugging D. All of the above ## Correct Answer: D. All of the above Rationale: Async/await offers improved error handling, readability, and debugging capabilities compared to using Promises directly. Fill-in-the-Blank: The await keyword can only be used within an __ function. ## Correct Answer: Asynchronous Rationale: await can only be used inside asynchronous functions declared with the async keyword to wait for Promises to be resolved. 1. Multiple Choice: Which keyword is used to define a function that can pause execution and resume at a later time in JavaScript? A) async B) await C) yield D) return ## Correct Answer: C) yield Rationale: The yield keyword in JavaScript allows for the creation of generators, which can be paused and resumed at a later time. 2. Fill in the Blank: Async functions always return a/an _____________. ## Correct Answer: Promise Rationale: Async functions always return a Promise, which resolves to the value returned by the async function or to the value it is rejected with. Rationale: The await keyword is used inside an async function to pause execution until the Promise is settled (resolved or rejected). 6. True/False: Using async/await in JavaScript guarantees faster execution of asynchronous tasks compared to using Promises. ## Correct Answer: False Rationale: While async/await can make asynchronous code easier to read and write, it does not guarantee faster execution compared to using Promises. 7. Multiple Choice: What is the purpose of the Promise.all method in JavaScript async operations? A) To execute promises in parallel B) To execute promises sequentially C) To handle errors in asynchronous code D) To convert synchronous code into asynchronous code ## Correct Answer: A) To execute promises in parallel Rationale: Promise.all is used to execute multiple promises in parallel and wait for all of them to settle before continuing execution. 8. Fill in the Blank: The async keyword is used to define a function that returns a/an __________. ## Correct Answer: Promise Rationale: Functions defined with the async keyword return a Promise, which resolves to the value returned by the async function. 9. True/False: The async keyword is used to handle asynchronous operations in JavaScript without blocking the main thread. ## Correct Answer: True Rationale: The async keyword allows JavaScript functions to perform asynchronous operations without blocking the main thread, enhancing performance. 10. Multiple Choice: Which statement correctly describes the behavior of async functions in JavaScript? A) They always run synchronously B) They cannot contain Promises C) They can be paused using the yield keyword D) They return Promises ## Correct Answer: D) They return Promises Rationale: Async functions always return Promises, which can be resolved with the value returned by the function or rejected with an error.