JavaScript Promises and Async/Await Quiz

Create an account and save your quiz results

Login and save your results

OR

Question 1/15

Which of the following is true about using `async` functions?

Select your answer

Question 2/15

What will the value of `result` be in the following code snippet?
async function example() {
  try {
    const result = await Promise.reject('Error occurred');
  } catch (error) {
    return error;
  }
}
example().then(console.log);

Select your answer

Question 3/15

What will the following code output?
async function test() {
  return await Promise.resolve('Hello');
}
test().then(console.log);

Select your answer

Question 4/15

What does the following code print to the console?
async function test() {
  await null;
  console.log('printed');
}
test();

Select your answer

Question 5/15

What would the output be after the execution of the following code?
async function process() {
  const p = new Promise(resolve => resolve('done'));
  return p.finally(() => 'cleaned');
}
process().then(console.log);

Select your answer

Question 6/15

What values are logged by the following code?
async function main() {
  console.log('start');
  setTimeout(() => console.log('middle'), 0);
  await Promise.resolve('end');
  console.log('end');
}
main();

Select your answer

Question 7/15

Which of these statements about error handling in async functions is true?

Select your answer

Question 8/15

Which of the following statements correctly describes `Promise.allSettled()`?

Select your answer

Question 9/15

Observe the code below. What will the logs be?
async function appFlow() {
  const result = await new Promise(resolve => setTimeout(() => resolve('Flowed'), 500));
  console.log(result);
}
appFlow();
console.log('Started');

Select your answer

Question 10/15

Which option correctly demonstrates how to handle multiple promise rejections?

Select your answer

Question 11/15

What will be the console output of this JavaScript code?
async function run() {
  throw new Error('Something went wrong.');
}

run().catch(error => console.error(error.message));
console.log('End');

Select your answer

Question 12/15

In what scenario would using `Promise.race()` be beneficial?

Select your answer

Question 13/15

How many times does 'A' get printed in the console when the following code is executed?
async function main() {
  console.log('A');
  await new Promise(resolve => setTimeout(resolve, 1000));
  console.log('A');
}
main();
console.log('A');

Select your answer

Question 14/15

What will be the console output after the following code snippet?
async function func() {
  return 'hello world';
}

debugger;
console.log(await func());

Select your answer

Question 15/15

What is the output of the following code?
async function mainProcess() {
  return new Promise(resolve => resolve('Success!'));
}

mainProcess().then(console.log);
console.log('Processing');

Select your answer

Your Results

You did not answer any questions correctly.

Your Answers

Question 1/15
😊 Your answer was correct 🙁 Your answer was incorrect
Which of the following is true about using `async` functions?

Available answers

An `async` function always returns a Promise, regardless of whether any `await` expressions are used within it. The presence of `await` expressions does not change the fact that the function as a whole is asynchronous.
Question 2/15
😊 Your answer was correct 🙁 Your answer was incorrect
What will the value of `result` be in the following code snippet?
async function example() {
  try {
    const result = await Promise.reject('Error occurred');
  } catch (error) {
    return error;
  }
}
example().then(console.log);

Available answers

In this example, `await Promise.reject('Error occurred')` will throw an error which is caught by the `catch` block, returning the error message as `result`. Therefore, `example().then(console.log)` will log 'Error occurred'.
Question 3/15
😊 Your answer was correct 🙁 Your answer was incorrect
What will the following code output?
async function test() {
  return await Promise.resolve('Hello');
}
test().then(console.log);

Available answers

The function `test` is an async function that immediately returns the result of `await Promise.resolve('Hello')`. Even though `await` is used, the resolved value is directly returned as the function's resolved promise, so `test().then(console.log)` will log 'Hello' to the console.
Question 4/15
😊 Your answer was correct 🙁 Your answer was incorrect
What does the following code print to the console?
async function test() {
  await null;
  console.log('printed');
}
test();

Available answers

In this code, `await null;` is effectively a no-operation in terms of async handling. It does not throw an error, and the function proceeds to the next line, which is the `console.log('printed')`. Therefore, 'printed' is logged to the console.
Question 5/15
😊 Your answer was correct 🙁 Your answer was incorrect
What would the output be after the execution of the following code?
async function process() {
  const p = new Promise(resolve => resolve('done'));
  return p.finally(() => 'cleaned');
}
process().then(console.log);

Available answers

The `finally` method does not alter the value with which the promise resolves or rejects. Hence, the promise resolves with 'done', which is why 'done' is logged.
Question 6/15
😊 Your answer was correct 🙁 Your answer was incorrect
What values are logged by the following code?
async function main() {
  console.log('start');
  setTimeout(() => console.log('middle'), 0);
  await Promise.resolve('end');
  console.log('end');
}
main();

Available answers

The execution order is 'start', then the microtask queue resolves the awaited promise logging 'end', and finally the setTimeout callback logs 'middle'. Microtasks (e.g., promise callbacks) are executed before the next task (from the task queue, such as `setTimeout`).
Question 7/15
😊 Your answer was correct 🙁 Your answer was incorrect
Which of these statements about error handling in async functions is true?

Available answers

You can use `try` and `catch` blocks to handle errors that occur in async functions due to rejected promises or explicit exceptions thrown within the function body.
Question 8/15
😊 Your answer was correct 🙁 Your answer was incorrect
Which of the following statements correctly describes `Promise.allSettled()`?

Available answers

The `Promise.allSettled()` method returns a promise that resolves after all of the given promises have either resolved or rejected, with an array of objects that each describe the outcome of each promise.
Question 9/15
😊 Your answer was correct 🙁 Your answer was incorrect
Observe the code below. What will the logs be?
async function appFlow() {
  const result = await new Promise(resolve => setTimeout(() => resolve('Flowed'), 500));
  console.log(result);
}
appFlow();
console.log('Started');

Available answers

'Started' is logged immediately after `appFlow()` before the promise resolves. After 500ms, 'Flowed' is logged when the promise inside `appFlow()` resolves.
Question 10/15
😊 Your answer was correct 🙁 Your answer was incorrect
Which option correctly demonstrates how to handle multiple promise rejections?

Available answers

`Promise.allSettled(promisesArray)` will return a promise that resolves after all input promises have settled, regardless of whether they fulfilled or rejected, allowing us to handle each promise's result individually.
Question 11/15
😊 Your answer was correct 🙁 Your answer was incorrect
What will be the console output of this JavaScript code?
async function run() {
  throw new Error('Something went wrong.');
}

run().catch(error => console.error(error.message));
console.log('End');

Available answers

The `run` function throws an error, which is caught and logged by the `.catch(error => console.error(error.message))` method. 'End' from `console.log('End')` is logged first because it is synchronous, and the error message is logged immediately after.
Question 12/15
😊 Your answer was correct 🙁 Your answer was incorrect
In what scenario would using `Promise.race()` be beneficial?

Available answers

`Promise.race()` returns the promise from the array that settles first, whether it is resolved or rejected. It's used when you need a result quickly from a series of potential sources.
Question 13/15
😊 Your answer was correct 🙁 Your answer was incorrect
How many times does 'A' get printed in the console when the following code is executed?
async function main() {
  console.log('A');
  await new Promise(resolve => setTimeout(resolve, 1000));
  console.log('A');
}
main();
console.log('A');

Available answers

The console will log 'A' three times. First, when console.log('A') is called in the `main` function, then immediately after `main()` is called, and finally after the 1-second delay with `setTimeout`.
Question 14/15
😊 Your answer was correct 🙁 Your answer was incorrect
What will be the console output after the following code snippet?
async function func() {
  return 'hello world';
}

debugger;
console.log(await func());

Available answers

The `debugger;` statement pauses execution, allowing for inspection. Once resumed, `console.log(await func())` logs the resolved promise value, 'hello world'.
Question 15/15
😊 Your answer was correct 🙁 Your answer was incorrect
What is the output of the following code?
async function mainProcess() {
  return new Promise(resolve => resolve('Success!'));
}

mainProcess().then(console.log);
console.log('Processing');

Available answers

'Processing' is printed first as it is logged immediately after `mainProcess()`, and then 'Success!' is logged once the promise resolves.