I am learning about the JavaScript async / await
API, and implementing the example code from MDN.
In one section, detailing the differences between async/await
and promise.then()
, the following is written:
var resolveAfter2Seconds = function() {
console.log("starting slow promise");
return new Promise(resolve => {
setTimeout(function() {
resolve(20);
console.log("slow promise is done");
}, 2000);
});
};
var parallel = function() {
console.log('==PARALLEL with Promise.then==');
resolveAfter2Seconds().then((message)=>console.log(message));
// in this case could be simply written as console.log(resolveAfter2Seconds());
}
setTimeout(parallel, 10000);
I was suspicious about the bit that says
// in this case could be simply written as console.log(resolveAfter2Seconds())
So, I ran the code and got the following:
starting slow promise
Promise { <pending> }
slow promise is done
Is the MDN example code comment trying to imply that instead of Promise { <pending> }
, I should see 20
? Why?