--- title: Expression async function slug: Web/JavaScript/Reference/Operators/async_function tags: - Function - JavaScript - Opérateur - Reference translation_of: Web/JavaScript/Reference/Operators/async_function original_slug: Web/JavaScript/Reference/Opérateurs/async_function ---
Le mot-clé async function
peut être utilisé pour définir une fonction asynchrone au sein d'une expression.
Note : Il est aussi possible de définir une fonction asynchrone en utilisant une instruction async function
.
async function [name]([param1[, param2[, ..., paramN]]]) { instructions }
name
paramN
instructions
Note : À partir d'ES2015 (ES6), il est aussi possible d'utiliser des fonctions fléchées pour les expressions de fonction asynchrone.
Une expression async function
est très proche, et partage quasiment la même syntaxe avec {{jsxref('Instructions/async_function', 'une instruction async function',"",1)}}. La différence principale entre une expression async function
et une instruction async function
est qu'on peut omettre le nom de la fonction dans les expressions async function
. On peut donc utiliser une expression async function
afin de créer une IIFE (pour Immediately Invoked Function Expression) qu'on appelle au moment de sa définition. Voir également le chapitre sur les fonctions pour plus d'informations.
function resolveAfter2Seconds(x) { return new Promise(resolve => { setTimeout(() => { resolve(x); }, 2000); }); }; (async function(x) { // fonction asynchrone immédiatement appelée var a = resolveAfter2Seconds(20); var b = resolveAfter2Seconds(30); return x + await a + await b; })(10).then(v => { console.log(v); // affiche 60 après 2 secondes. }); var add = async function(x) { var a = await resolveAfter2Seconds(20); var b = await resolveAfter2Seconds(30); return x + a + b; }; add(10).then(v => { console.log(v); // affiche 60 après 4 secondes. });
Spécification | État | Commentaires |
---|---|---|
{{SpecName('ESDraft', '#sec-async-function-definitions', 'async function')}} | {{Spec2('ESDraft')}} | |
{{SpecName('ES2018', '#sec-async-function-definitions', 'async function')}} | {{Spec2('ES2018')}} | |
{{SpecName('ES2017', '#sec-async-function-definitions', 'async function')}} | {{Spec2('ES2017')}} | Définition initiale. |
{{Compat("javascript.operators.async_function_expression")}}