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 2020å¹´1æ.
[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® 2027 Language Specification> # sec-asynciteratorprototype-asynciterator> |