AsyncFunction
构造函数
creates a new
async function
object. In JavaScript, every asynchronous function is actually an
AsyncFunction
对象。
注意,
AsyncFunction
is
not
a global object. It can be obtained with the following code:
Object.getPrototypeOf(async function(){}).constructor
new AsyncFunction([arg1[, arg2[, ...argN]],] functionBody)
arg1, arg2, ... arg
N
x
", "
theValue
", or "
a,b
".
functionBody
A string containing the JavaScript statements comprising the function definition.
async function
objects created with the
AsyncFunction
constructor are parsed when the function is created. This is less efficient than declaring an async function with an
异步函数表达式
and calling it within your code, because such functions are parsed with the rest of the code.
All arguments passed to the function are treated as the names of the identifiers of the parameters in the function to be created, in the order in which they are passed.
注意:
async functions
created with the
AsyncFunction
constructor do not create closures to their creation contexts; they are always created in the global scope.
When running them, they will only be able to access their own local variables and global ones, not the ones from the scope in which the
AsyncFunction
constructor was called.
This is different from using
eval
with code for an async function expression.
援引
AsyncFunction
构造函数作为函数 (不使用
new
运算符) 有相同效果若把它作为构造函数进行援引。
function resolveAfter2Seconds(x) {
return new Promise(resolve => {
setTimeout(() => {
resolve(x);
}, 2000);
});
}
let AsyncFunction = Object.getPrototypeOf(async function(){}).constructor
let a = new AsyncFunction('a',
'b',
'return await resolveAfter2Seconds(a) + await resolveAfter2Seconds(b);');
a(10, 20).then(v => {
console.log(v); // prints 30 after 4 seconds
});
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'AsyncFunction object' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
AsyncFunction
|
Chrome 55 | Edge 15 | Firefox 52 | IE No | Opera 42 | Safari 10.1 | WebView Android 55 | Chrome Android 55 | Firefox Android 52 | Opera Android 42 | Safari iOS 10.3 | Samsung Internet Android 6.0 |
nodejs
7.6.0
|
完整支持
不支持
用户必须明确启用此特征。
AsyncFunction
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()