pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/async_function*

://developer.mozilla.org/favicon.ico" />

このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

async function* 式

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2020年1月.

async function* キーワードは、式の内部で非同期ジェネレーター関数を定義するために使用できます。

また、 async function* 宣言 を使って非同期ジェネレーター関数を定義することもできます。

試してみましょう

async function joinAll(generator) {
  let str = "";
  for await (const val of generator()) {
    str += val;
  }
  return str;
}
joinAll(async function* () {
  yield await Promise.resolve("a");
  yield await Promise.resolve("b");
  yield await Promise.resolve("c");
}).then((str) => console.log(str));
// 予想される結果: "abc"

構文

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

async function* name(param0) {
  statements
}
async function* name(param0, param1) {
  statements
}
async function* name(param0, param1, /* …, */ paramN) {
  statements
}

メモ: 式文 は、 async function* 宣言との曖昧さを避けるために、キーワード async function で始めることはできません。 async function キーワードは、文を受け付けないコンテキストに現れたときのみ、式を開始します。

引数

name 省略可

関数名。省略可能で、その場合関数は無名となります。この名前は関数本体に対してのみローカルです。

paramN 省略可

関数の正式な引数の名前です。引数の構文については、関数リファレンスを参照してください。

statements 省略可

関数本体を構成する文です。

解説

async function* 式は async function* 宣言 ととても似ており、ほとんど同じ構文を持っています。 async function* 式と async function* 宣言の主な違いは関数名で、 async function* 式では無名関数を作成するために省略することができます。 async function* 式は定義されるとすぐに実行される IIFE (Immediately Invoked Function Expression) として使うことができ、アドホックな非同期反復可能オブジェクトを作成することができます。詳しくは関数の章も参照してください。

async function* 式の使用

次の例では、無名の非同期ジェネレーター関数を定義し、それを x に代入しています。この関数は引数の2乗を返します。

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

仕様書

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

ブラウザーの互換性

関連情報

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy