AsyncFunction 构造函数用来创建新的 异步函数 对象,JavaScript 中每个异步函数都是 AsyncFunction 的对象。
注意,AsyncFunction 并不是一个全局对象,需要通过下面的方法来获取:
Object.getPrototypeOf(async function(){}).constructor
语法
new AsyncFunction([arg1[, arg2[, ...argN]],] functionBody)
参数
-
arg1, arg2, ... argN -
函数的参数名,它们是符合 JavaScript 标示符规范的一个或多个用逗号隔开的字符串。例如
x、theValue、或a,b。 -
functionBody - 一段字符串形式的 JavaScript 语句,这些语句组成了新函数的定义。
描述
执行 AsyncFunction 构造函数的时候,会创建一个 异步函数 对象来的高效,因为第二种方式中异步函数是与其他代码一起被解释器解析的,而第一种方式的函数体是单独解析的。
传递给 AsyncFunction 构造函数的所有参数,都会成为新函数中的变量,变量的名称和定义顺序与各参数相同。
注意:使用 AsyncFunction 构造函数创建的eval 不同的地方。
调用 AsyncFunction 构造函数时可以省略 new,其效果是一样的。
属性
-
AsyncFunction.length -
AsyncFunction构造函数的 length 属性,值为 1。 -
AsyncFunction.prototype - 通过原型对象可以为所有异步函数对象定义额外的属性。
AsyncFunction 原型对象
属性
-
AsyncFunction.constructor -
默认值为
AsyncFunction。 -
AsyncFunction.prototype[@@toStringTag] - 返回 "AsyncFunction"。
AsyncFunction 实例
AsyncFunction 实例继承了 AsyncFunction.prototype 的方法和属性。和所有构造函数一样,修改 AsyncFunction 构造函数的原型对象会同时对所有 AsyncFunction 实例上生效。
示例
通过 AsyncFunction 构造器创建一个异步函数
function resolveAfter2Seconds(x) {
return new Promise(resolve => {
setTimeout(() => {
resolve(x);
}, 2000);
});
}
var AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
var a = new AsyncFunction('a',
'b',
'return await resolveAfter2Seconds(a) + await resolveAfter2Seconds(b);');
a(10, 20).then(v => {
console.log(v); // 4 秒后打印 30
});
规范
| Specification | Status | Comment |
|---|---|---|
| ECMAScript Latest Draft (ECMA-262) AsyncFunction object |
Draft | ES2017 中的初始定义 |
浏览器兼容性
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out
https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
AsyncFunction |
Chrome Full support 55 | Edge Full support 15 | Firefox Full support 52 | IE No support No | Opera Full support 42 | Safari ? | WebView Android Full support 55 | Chrome Android Full support 55 | Firefox Android Full support 52 | Opera Android Full support 42 | Safari iOS ? | Samsung Internet Android Full support 6.0 | nodejs Full support 7.6.0
|
prototype |
Chrome Full support 55 | Edge Full support 15 | Firefox Full support 52 | IE No support No | Opera Full support 42 | Safari ? | WebView Android Full support 55 | Chrome Android Full support 55 | Firefox Android Full support 52 | Opera Android Full support 42 | Safari iOS ? | Samsung Internet Android Full support 6.0 | nodejs Full support 7.6.0
|
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- User must explicitly enable this feature.
- User must explicitly enable this feature.