JavaScript Promises and Async/Await Quiz

Create an account and save your quiz results

Login and save your results

OR

Question 1/15

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

Select your answer

Question 2/15

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

Select your answer

Question 3/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 4/15

Why might using `Promise.all()` lead to performance issues compared to `Promise.allSettled()`?

Select your answer

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

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

Select your answer

Question 7/15

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

Select your answer

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

What will be the value of `x` after executing the following code?
let x = 10;
(async function() {
  x += await 3;
  console.log(x);
})();

Select your answer

Question 11/15

When running this code, what will be logged to the console?
async function exampleFunction() {
  try {
    return await Promise.reject('Failed');
  } catch (e) {
    console.log(e);
  }
}
exampleFunction();

Select your answer

Question 12/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

Question 13/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 14/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 15/15

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

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
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 2/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 3/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 4/15
😊 Your answer was correct 🙁 Your answer was incorrect
Why might using `Promise.all()` lead to performance issues compared to `Promise.allSettled()`?

Available answers

`Promise.all()` will reject immediately when any of the input promises rejects. This can lead to issues if you're performing multiple independent operations, and `Promise.allSettled()` is preferred to handle these scenarios where all outcomes are significant.
Question 5/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 6/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 7/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 8/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 9/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 10/15
😊 Your answer was correct 🙁 Your answer was incorrect
What will be the value of `x` after executing the following code?
let x = 10;
(async function() {
  x += await 3;
  console.log(x);
})();

Available answers

The right-hand side of the `+=` operator is `await 3`, which resolves to `3` as a resolved promise is created for any non-promise value. Thus, the value of `x` becomes `13`.
Question 11/15
😊 Your answer was correct 🙁 Your answer was incorrect
When running this code, what will be logged to the console?
async function exampleFunction() {
  try {
    return await Promise.reject('Failed');
  } catch (e) {
    console.log(e);
  }
}
exampleFunction();

Available answers

The `Promise.reject('Failed')` throws an error, which is caught in the `catch (e)` block. Therefore, 'Failed' is logged to the console.
Question 12/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.
Question 13/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 14/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 15/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.