我正在学习JavaScript async / await
API,并实现example code from MDN.
在一个小节中,详细介绍了async/await
和promise.then()
之间的区别,内容如下:
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);
我对所说的
表示怀疑//在这种情况下可以简单地写成 console.log(resolveAfter2Seconds())
因此,我运行了代码并得到了以下内容:
starting slow promise
Promise { <pending> }
slow promise is done
MDN示例代码注释是否试图暗示我应该看到Promise { <pending> }
而不是20
?为什么?