From a55b575e8089ee6cab7c5c262a7e6db55d0e34d6 Mon Sep 17 00:00:00 2001 From: Florian Merz Date: Thu, 11 Feb 2021 14:46:50 +0100 Subject: unslug es: move --- .../global_objects/asyncfunction/index.html | 122 +++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 files/es/web/javascript/reference/global_objects/asyncfunction/index.html (limited to 'files/es/web/javascript/reference/global_objects/asyncfunction') diff --git a/files/es/web/javascript/reference/global_objects/asyncfunction/index.html b/files/es/web/javascript/reference/global_objects/asyncfunction/index.html new file mode 100644 index 0000000000..bdb37b012c --- /dev/null +++ b/files/es/web/javascript/reference/global_objects/asyncfunction/index.html @@ -0,0 +1,122 @@ +--- +title: Funciones asíncronas +slug: Web/JavaScript/Referencia/Objetos_globales/Funcionesasíncronas +translation_of: Web/JavaScript/Reference/Global_Objects/AsyncFunction +--- +
{{JSRef}}
+ +
El constructor de las funciones asíncronas crea un nuevo objecto {{jsxref("Statements/async_function", "async function")}}. 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)
+ +

Parametros

+ +
+
arg1, arg2, ... argN
+
Nombres a ser usados por la función como los nombres de los argumentos. Cada uno debe ser una cadena que corresponda a un identificador válido en JavaScript o una lista de cadenas separadas por coma, ejemplo: "x", "theValue", or "a,b".
+
functionBody
+
Una cadena que contiene las declaraciones de JavaScript que comprenden la definición de la función.
+
+ +

Descripción

+ +

{{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.

+ +

Propiedades

+ +
+
AsyncFunction.length
+
The AsyncFunction constructor's length property whose value is 1.
+
{{jsxref("AsyncFunction.prototype")}}
+
Allows the addition of properties to all async function objects.
+
+ +

El objeto prototipo  AsyncFunction 

+ +

Propiedades

+ +
{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncFunction/prototype', 'Properties')}}
+ +

AsyncFunction y sus instancias

+ +

Una 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 .

+ +

Ejemplos

+ +

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
+});
+
+ +

Especificaciones

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ESDraft', '#sec-async-function-objects', 'AsyncFunction object')}}{{Spec2('ESDraft')}}Initial definition in ES2017.
+ +

Compatibilidad del navegador

+ +
+ + +

{{Compat("javascript.builtins.AsyncFunction")}}

+
+ +

Ver también

+ + -- cgit v1.2.3-54-g00ecf