AsyncIterator.prototype[Symbol.asyncIterator]()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.
[Symbol.asyncIterator]()
は AsyncIterator
インスタンスのメソッドで、非同期反復可能プロトコルを実装し、組み込みの非同期イテレーターを、 for await...of
ループのような非同期反復可能オブジェクトを期待するほとんどの構文で利用できるようにします。これは this
の値を返しますが、これは非同期イテレーターオブジェクトそのものです。
試してみましょう
const map1 = new Map();
map1.set("0", "foo");
map1.set(1, "bar");
const iterator1 = map1[Symbol.iterator]();
for (const item of iterator1) {
console.log(item);
}
// Expected output: Array ["0", "foo"]
// Expected output: Array [1, "bar"]
構文
asyncIterator[Symbol.asyncIterator]()
引数
なし。
返値
this
の値で、非同期イテレーターオブジェクトそのものです。
例
for await...of ループを使用した反復処理
このメソッドを直接呼び出す必要はほとんどないことに注意してください。[Symbol.asyncIterator]()
メソッドが存在することで、すべての組み込み非同期イテレーターを非同期反復可能にすることができます、また、 for await...of
ループなどの反復処理構文は、自動的にこのメソッドを呼び出し、ループ処理する非同期イテレーターを取得します。
const asyncIterator = (async function* () {
yield 1;
yield 2;
yield 3;
})();
(async () => {
for await (const value of asyncIterator) {
console.log(value);
}
})();
// ログ: 1, 2, 3
仕様書
Specification |
---|
ECMAScript® 2025 Language Specification # sec-asynciteratorprototype-asynciterator |
ブラウザーの互換性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
[Symbol.asyncIterator] |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support