【发布时间】:2016-04-16 20:16:51
【问题描述】:
我试图更好地理解 JavaScript 中的 async function 在技术上是什么意思,即使我基本上知道如何使用它们。
许多对 async/await 的介绍让人相信 async 函数基本上只是一个承诺,但显然情况并非如此(至少 Babel6-transpiled code 不是):
async function asyncFunc() {
// nop
}
var fooPromise = new Promise(r => setTimeout(r, 1));
console.clear();
console.log("typeof asyncFunc is", typeof asyncFunc); // function
console.log("typeof asyncFunc.next is", typeof asyncFunc.next); // undefined
console.log("typeof asyncFunc.then is", typeof asyncFunc.then); // undefined
console.log("typeof fooPromise is", typeof fooPromise); // object
console.log("typeof fooPromise.next is", typeof fooPromise.next); // undefined
console.log("typeof fooPromise.then is", typeof fooPromise.then); // function
不过,await 是绝对可能的,比如await fooPromise()。
async funtion是它自己的东西吗?await只是兼容承诺?还有,有没有办法在运行时区分简单的
function和async function(以 Babel 兼容的方式)?
【问题讨论】:
标签: javascript babeljs ecmascript-next