Search⌘ K
AI Features

Solution Review: Promises

Explore how JavaScript promises work, focusing on solution reviews with .then handlers and promise resolution. Learn to interpret asynchronous code and understand why certain outputs occur in promise chains, preparing you for related interview questions.

Question 1: Solution review

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

Javascript (babel-node)
function func1(){
return new Promise(function(resolve,reject){
setTimeout(function(){
resolve("Func1")
},1000)
})
}
function func2(){
return new Promise(function(resolve,reject){
setTimeout(function(){
resolve("Func2")
},2000)
})
}
func1()
.then(func2())
.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.

Node.js
function func1(){
return new Promise(function(resolve,reject){
setTimeout(function(){
resolve("Func1")
},1000)
})
}
function func2(){
return new Promise(function(resolve,reject){
setTimeout(function(){
resolve("Func2")
},2000)
})
}
func1()
.then(func2())
.then(function(result) {
console.log(result);
});

As you can see, the correct option is C. Let’s discuss the code to understand the answer.

On line 18, the func1 function executes, ...