--- title: Funciones asíncronas slug: Web/JavaScript/Reference/Global_Objects/AsyncFunction translation_of: Web/JavaScript/Reference/Global_Objects/AsyncFunction original_slug: Web/JavaScript/Referencia/Objetos_globales/Funcionesasíncronas ---
Object.getPrototypeOf(async function(){}).constructor
new AsyncFunction([arg1[, arg2[, ...argN]],] functionBody)
arg1, arg2, ... argN
x
", "theValue
", or "a,b
".functionBody
{{jsxref("Statements/async_function", "async function")}} los objetos creados con el constructor AsyncFunction
son analizados gramaticalmente en el momento que la función es creada. Esto es menos eficiente que declarar una función asincrona con un {{jsxref("Statements/async_function", "async function expression")}} y llamarla con eso en tu código , ya que las funciones están analizadas gramaticalmento junto al resto del código.
Todos los argumentos que son pasados por la función son tratados por los nombres de los identificadores con los que fueron creados, en el orden en que son pasados por la función.
Note: {{jsxref("Statements/async_function", "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 {{jsxref("Global_Objects/eval", "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.
AsyncFunction.length
AsyncFunction
constructor's length property whose value is 1. AsyncFunction
AsyncFunction
y sus instanciasUna instancia de AsyncFunction
hereda métodos y propiedades de {{jsxref("AsyncFunction.prototype")}}. Como son todos los constructores, puedes cambiar el constructor del objeto prototipo para aplicar cambios a todas las instancias de AsyncFunction
.
AsyncFunction
constructorfunction 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 });
Specification | Status | Comment |
---|---|---|
{{SpecName('ESDraft', '#sec-async-function-objects', 'AsyncFunction object')}} | {{Spec2('ESDraft')}} | Initial definition in ES2017. |
{{Compat("javascript.builtins.AsyncFunction")}}