打印JavaScript Promise的已解析值

时间:2018-10-23 19:00:02

标签: javascript promise es6-promise

我正在学习JavaScript async / await API,并实现example code from MDN.

在一个小节中,详细介绍了async/awaitpromise.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?为什么?

0 个答案:

没有答案