Search⌘ K
AI Features

Solution Review: Async & Await

Explore how to analyze async and await functions in JavaScript to understand promise resolution and execution order. This lesson reviews coding solutions that demonstrate concurrent versus sequential asynchronous calls, helping you grasp crucial concepts used in technical interviews.

Question 1: Solution review #

In the previous lesson, you were given the following code:

Javascript (babel-node)
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.

Javascript (babel-node)
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);
...