Solution Review: Async & Await
In this lesson, we will discuss the solutions to the questions in the previous lesson.
We'll cover the following...
Press + to interact
function func1(num) {return new Promise(function(resolve){setTimeout(function(){resolve(num);}, 1000);});}async function multiply(num) {const x = func1(10);const y = func1(3);return num * await x * await y;}multiply(10).then(function(result){console.log(result);});
For the code above, you had to answer the following question:
Explanation #
Run the code below to see the solution.
Press + to interact
function func1(num) {return new Promise(function(resolve){setTimeout(function(){resolve(num);}, 1000);});}async function multiply(num) {const x = func1(10);const y = func1(3);return num * await x * await y;}multiply(10).then(function(result){console.log(result);});
The code outputs 300
, making Option B the correct answer. Let’s understand the code to see why.
On line 15, we call the multiply
function with 10
passed as the argument.
async function multiply(num) {
const x = func1(10); //line 10
const y = func1(3);
...