JavaScript Async and Promise MCQ

1. What is a Promise in JavaScript?

a) A function that executes asynchronously
b) A callback function
c) An object representing the eventual completion or failure of an asynchronous operation
d) A way to handle events

Answer:

c) An object representing the eventual completion or failure of an asynchronous operation

Explanation:

A Promise in JavaScript is an object that represents the eventual completion (or failure) of an asynchronous operation, along with its resulting value.

2. How do you create a new Promise in JavaScript?

a) new Promise(function(resolve, reject) {…})
b) Promise.create(function(resolve, reject) {…})
c) new AsyncPromise(function(resolve, reject) {…})
d) Promise.new(function(resolve, reject) {…})

Answer:

a) new Promise(function(resolve, reject) {…})

Explanation:

A new Promise is created in JavaScript using the new Promise() constructor with a function that takes two arguments, resolve and reject.

3. What method is used to specify the code to execute after a Promise is fulfilled?

a) .then()
b) .catch()
c) .finally()
d) .done()

Answer:

a) .then()

Explanation:

The .then() method is used on a Promise to specify what to do when the promise is fulfilled or successfully resolved.

4. What does the .catch() method do in the context of a Promise?

a) Catches any errors that occurred in the Promise
b) Pauses the execution of the Promise
c) Executes when the Promise is resolved
d) Sends data to the Promise

Answer:

a) Catches any errors that occurred in the Promise

Explanation:

The .catch() method is used with a Promise to specify the action to be taken if the Promise is rejected or an error occurs.

5. How can you chain multiple asynchronous operations with Promises?

a) Using nested Promises
b) Using multiple .then() methods
c) Using the async keyword
d) By passing an array of Promises

Answer:

b) Using multiple .then() methods

Explanation:

Multiple asynchronous operations can be chained together using multiple .then() methods, where each one takes the output of the previous as its input.

6. What is the purpose of the .finally() method in a Promise?

a) To execute code after the Promise is settled, regardless of its outcome
b) To finalize the result of the Promise
c) To stop the Promise from executing further
d) To handle only successful resolutions

Answer:

a) To execute code after the Promise is settled, regardless of its outcome

Explanation:

The .finally() method is used to execute a block of code after the Promise is settled, whether it is fulfilled or rejected.

7. What does the Promise.all() method do?

a) Waits for all passed Promises to be settled
b) Executes all Promises in parallel
c) Rejects all Promises
d) Resolves the first Promise that completes

Answer:

a) Waits for all passed Promises to be settled

Explanation:

Promise.all() takes an iterable of Promises and resolves when all of the passed Promises have resolved or rejects as soon as one of them rejects.

8. What is the async keyword used for in JavaScript?

a) To declare an asynchronous function
b) To make a function return a Promise
c) To pause the execution of a function
d) Both a and b

Answer:

d) Both a and b

Explanation:

The async keyword is used to declare an asynchronous function, which automatically transforms the function to return a Promise.

9. How do you handle errors in an async function?

a) Using a try-catch block
b) Using the .catch() method
c) Using the onError handler
d) Both a and b

Answer:

d) Both a and b

Explanation:

Errors in an async function can be handled using a try-catch block within the function or using a .catch() method on the Promise returned by the async function.

10. What is the await keyword used for in async functions?

a) To pause the execution of the async function until a Promise settles
b) To automatically catch errors
c) To create a new Promise
d) To repeat an asynchronous operation

Answer:

a) To pause the execution of the async function until a Promise settles

Explanation:

The await keyword is used inside an async function to pause its execution until the Promise is resolved or rejected.

11. Can the await keyword be used outside of an async function?

a) Yes, in any function
b) Yes, but only in top-level code
c) No, it must be used within an async function
d) Yes, in any asynchronous code

Answer:

c) No, it must be used within an async function

Explanation:

The await keyword can only be used inside an async function and is not allowed in regular functions or outside of a function.

12. What is the result of an async function execution?

a) An immediate value
b) A synchronous result
c) A Promise
d) Undefined

Answer:

c) A Promise

Explanation:

An async function returns a Promise, which will eventually resolve with the value returned by the function.

13. What happens when a Promise is rejected inside an async function without a catch block?

a) The Promise is ignored
b) An uncaught error is thrown
c) The function returns null
d) The Promise is resolved with undefined

Answer:

b) An uncaught error is thrown

Explanation:

If a Promise is rejected inside an async function without proper error handling (like a catch block), it results in an uncaught error.

14. How does Promise.resolve() work?

a) It creates a new Promise that is immediately resolved
b) It rejects a Promise
c) It pauses a Promise
d) It combines multiple Promises

Answer:

a) It creates a new Promise that is immediately resolved

Explanation:

Promise.resolve() creates a new Promise that is immediately resolved with the given value.

15. How can you convert a callback-based function to return a Promise?

a) Using the Promise constructor
b) By wrapping the function with async
c) Using Promise.resolve()
d) By changing the function to an arrow function

Answer:

a) Using the Promise constructor

Explanation:

To convert a callback-based function to return a Promise, you can wrap the function call inside a new Promise using the Promise constructor.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top