Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Cette page a été traduite à partir de l'anglais par la communauté. Vous pouvez également contribuer en rejoignant la communauté francophone sur MDN Web Docs.

Expression async function*

BaselineWidely available

Les mots-clésasync function* peuvent être utilisés afin de créer une fonction génératrice asynchrone au sein d'une expression.

Exemple interactif

async function* foo() {  yield await Promise.resolve("a");  yield await Promise.resolve("b");  yield await Promise.resolve("c");}let str = "";async function generate() {  for await (const val of foo()) {    str = str + val;  }  console.log(str);}generate();// Expected output: "abc"

Syntaxe

js
async function* (param0) {  instructions}async function* (param0, param1) {  instructions}async function* (param0, param1, /* … ,*/ paramN) {  instructions}async function* nom(param0) {  instructions}async function* nom(param0, param1) {  instructions}async function* nom(param0, param1, /* … ,*/ paramN) {  instructions}

Paramètres

nomFacultatif

Le nom de la fonction. S'il est absent, la fonction estanonyme. Le nom est uniquement local au corps de la fonction.

paramNFacultatif

Le nom d'un argument à passer à la fonction. Une fonction peut avoir jusqu'à 255 arguments.

instructionsFacultatif

Les instructions qui forment le corps de la fonction.

Description

Une expressionasync function* est très proche et possède quasiment la même syntaxe qu'uneinstructionasync function*. La différence principale entre une expressionasync function* et une instructionasync function* est quele nom de la fonction peut être omis dans les expressionsasync function* afin de créer des fonctions génératrices asynchronesanonymes. Voir aussi le chapitre surles fonctions en JavaScript pour plus d'informations.

Exemples

Utiliser async function*

L'exemple qui suit définit une fonction génératrice asynchrone anonyme et l'affecte àx. Cette fonction génère le carré de son argument :

js
const x = async function* (y) {  yield Promise.resolve(y * y);};x(6)  .next()  .then((res) => console.log(res.value)); // affiche 36

Spécifications

Specification
ECMAScript® 2026 Language Specification
# sec-async-generator-function-definitions

Compatibilité des navigateurs

Voir aussi

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp