Esta traducción está incompleta. Por favor, ayuda a traducir este artículo del inglés.

El constructor de las funciones asíncronas crea un nuevo async function objeto. En JavaScript cada función asíncrona es un objeto AsyncFunction.
 
Nota: AsyncFunction no es un objeto global. Este puede ser obtenido como resultado del siguiente código.
 
Object.getPrototypeOf(async function(){}).constructor

Syntax

new AsyncFunction([arg1[, arg2[, ...argN]],] functionBody)

Parameters

arg1, arg2, ... argN
Nombres a ser usados por la función como los nombres de los argumentos. Cada uno debe ser un string que corresponda a un identificador válido en JavaScript o una lista de estrings cuyos strings estén separado por coma, ejemplo: "x", "theValue", or "a,b".
functionBody
A string containing the JavaScript statements comprising the function definition.

Description

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 async function expression 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.

Note: 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.

Invoking the AsyncFunction constructor as a function (without using the new operator) has the same effect as invoking it as a constructor.

Properties

AsyncFunction.length
The AsyncFunction constructor's length property whose value is 1.
AsyncFunction.prototype
Allows the addition of properties to all async function objects.

AsyncFunction prototype object

Properties

AsyncFunction.constructor
The initial value is AsyncFunction.
AsyncFunction.prototype[@@toStringTag]
Returns "AsyncFunction".

AsyncFunction instances

AsyncFunction instances inherit methods and properties from AsyncFunction.prototype. As with all constructors, you can change the constructor's prototype object to make changes to all AsyncFunction instances.

Examples

Creating an async function from an AsyncFunction constructor

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); // prints 30 after 4 seconds
});

Specifications

Specification Status Comment
ECMAScript Latest Draft (ECMA-262)
La definición de 'AsyncFunction object' en esta especificación.
Draft Initial definition in ES2017.

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Soporte básico55 ?52 No42 ?
prototype55 ?52 No42 ?
FeatureAndroid webviewChrome para AndroidEdge mobileFirefox para AndroidOpera AndroidiOS SafariSamsung Internet
Soporte básico5555 ?5242 ?6.0
prototype ?55 ?5242 ?6.0

See also

Etiquetas y colaboradores del documento

Colaboradores en esta página: miguelrijo
Última actualización por: miguelrijo,