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 these statements about error handling in async functions is true?

Select your answer

Question 2/15

Determine the correct outcome for this scenario.
let data;
async function fetchData() {
  const result = await Promise.resolve(42);
  data = result;
}
fetchData();
console.log(data);

Select your answer

Question 3/15

Which of the following statements accurately describes `Promise.any()`?

Select your answer

Question 4/15

Given the following code, what does `someAsyncFunction` return?
function someAsyncFunction() {
  return Promise.resolve(123);
}
someAsyncFunction();

Select your answer

Question 5/15

Why does the following code snippet cause an error?
async function start() {
  try {
    await JSON.parse('{ invalidJson }');
  } catch (error) {
    console.error('Caught Error:', error.message);
  }
}
start();

Select your answer

Question 6/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 7/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 8/15

What would be the final output of this code?
async function main() {
  try {
    const response = await fetch('https://example.com');
    console.log('Response received');
  } catch (error) {
    console.log('Fetch error');
  }
  console.log('Completed');
}
main();

Select your answer

Question 9/15

Why does the following code not catch any errors?
async function example() {
  try {
    let data = await fetchNonExistent();
    console.log(data);
  } catch {
    console.log('An error occurred');
  }
}
example();

Select your answer

Question 10/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 11/15

Why is the following code considered bad practice?
async function loadData() {
  fetch('https://example.com/api1').then(response => response.json());
  fetch('https://example.com/api2').then(response => response.json());
}

Select your answer

Question 12/15

Which option correctly demonstrates how to handle multiple promise rejections?

Select your answer

Question 13/15

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

Select your answer

Question 14/15

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

Select your answer

Question 15/15

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

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 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 2/15
😊 Your answer was correct 🙁 Your answer was incorrect
Determine the correct outcome for this scenario.
let data;
async function fetchData() {
  const result = await Promise.resolve(42);
  data = result;
}
fetchData();
console.log(data);

Available answers

Immediately after `fetchData()` is invoked, `console.log(data);` runs before the promise resolves. Hence, `data` is `undefined` at the time of the log.
Question 3/15
😊 Your answer was correct 🙁 Your answer was incorrect
Which of the following statements accurately describes `Promise.any()`?

Available answers

`Promise.any()` returns a promise that resolves as soon as any of the promises in the iterable is resolved. If all of the promises are rejected, it returns an AggregateError.
Question 4/15
😊 Your answer was correct 🙁 Your answer was incorrect
Given the following code, what does `someAsyncFunction` return?
function someAsyncFunction() {
  return Promise.resolve(123);
}
someAsyncFunction();

Available answers

The `someAsyncFunction` explicitly returns a Promise object that resolves with the value of 123. Therefore, the function call returns a promise that resolves to 123.
Question 5/15
😊 Your answer was correct 🙁 Your answer was incorrect
Why does the following code snippet cause an error?
async function start() {
  try {
    await JSON.parse('{ invalidJson }');
  } catch (error) {
    console.error('Caught Error:', error.message);
  }
}
start();

Available answers

The error occurs because `JSON.parse` is a synchronous operation, and exceptions thrown are not converted into promise rejections, thus they don't get caught by the `await`. However, the `try...catch` still captures it.
Question 6/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 7/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 8/15
😊 Your answer was correct 🙁 Your answer was incorrect
What would be the final output of this code?
async function main() {
  try {
    const response = await fetch('https://example.com');
    console.log('Response received');
  } catch (error) {
    console.log('Fetch error');
  }
  console.log('Completed');
}
main();

Available answers

If the fetch operation succeeds, 'Response received' is logged, followed by 'Completed'. The `catch` block won't execute here. The final expected output is 'Response received, Completed'.
Question 9/15
😊 Your answer was correct 🙁 Your answer was incorrect
Why does the following code not catch any errors?
async function example() {
  try {
    let data = await fetchNonExistent();
    console.log(data);
  } catch {
    console.log('An error occurred');
  }
}
example();

Available answers

`fetchNonExistent` is not a defined function, so the error 'ReferenceError' is thrown even before the promise handling can be processed, leading to an immediate exit from the async function.
Question 10/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 11/15
😊 Your answer was correct 🙁 Your answer was incorrect
Why is the following code considered bad practice?
async function loadData() {
  fetch('https://example.com/api1').then(response => response.json());
  fetch('https://example.com/api2').then(response => response.json());
}

Available answers

In the provided code, the fetch operations are asynchronous, and by not using `await` or returning the promises, any potential errors during fetching or JSON parsing are not handled, which can result in unhandled promise rejections.
Question 12/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 13/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 14/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 15/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.