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* ì ì¸ì 주ì´ì§ ì´ë¦ì¼ë¡ ì ë¹ë기 ì ëë ì´í° í¨ìì bindingì ìì±í©ëë¤.
ëí async function* ííìì ì¬ì©í´ì ë¹ë기 ì ëë ì´í° í¨ì를 ì ì¸í ì ììµëë¤.
ìëí´ ë³´ê¸°
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 += val;
}
console.log(str);
}
generate();
// Expected output: "abc"
구문
async function* name(param0) {
statements
}
async function* name(param0, param1) {
statements
}
async function* name(param0, param1, /* â¦, */ paramN) {
statements
}
ì°¸ê³ : ë¹ë기 ì ëë ì´í° í¨ìë ê·¸ì ëìíë íì´í í¨ì ííê° ììµëë¤.
ì°¸ê³ :
functionê³¼ *ë ë³ê°ì í í°ì´ë¯ë¡ 공백ì´ë ì¤ë°ê¿ 문ìë¡ ë¶ë¦¬í ì ììµëë¤. íì§ë§ asyncì function ì¬ì´ìë ì¤ë°ê¿ 문ìê° ì¬ ì ììµëë¤. ê·¸ë ì§ ìì¼ë©´ ì¸ë¯¸ì½ë¡ ì´ ìëì¼ë¡ ì½ì
ëì´, asyncë ìë³ìê° ëê³ ë머ì§ë function* ì ì¸ì´ ë©ëë¤.
매ê°ë³ì
name-
í¨ìì ì´ë¦.
paramOptional-
í¨ìì íì 매ê°ë³ì ì´ë¦ì ëë¤. 매ê°ë³ì 구문ì í¨ì ë í¼ë°ì¤ë¥¼ íì¸íì¸ì.
statementsOptional-
í¨ì 본문ì 구ì±íë ëª ë ¹ë¬¸ì ëë¤.
ì¤ëª
async function* ì ì¸ì AsyncGeneratorFunction ê°ì²´ë¥¼ ìì±í©ëë¤. ë¹ë기 ì ëë ì´í° í¨ìê° í¸ì¶ë ëë§ë¤ ë¹ë기 ë°ë³µì íë¡í ì½ì ë°ë¥´ë ìë¡ì´ AsyncGenerator ê°ì²´ë¥¼ ë°íí©ëë¤. next()를 í¸ì¶í ëë§ë¤ ë°ë³µì ê²°ê³¼ ê°ì²´ë¡ ì´íëë Promise를 ë°íí©ëë¤.
ë¹ë기 ì ëë ì´í° í¨ìë ë¹ë기 í¨ìì ì ëë ì´í° í¨ìì 기ë¥ì ê²°í©í ê²ì
ëë¤. í¨ì 본문 ë´ìì awaitì yield í¤ìë를 모ë ì¬ì©í ì ììµëë¤. ì´ë¥¼ íµí´ awaitë¡ ë¹ë기 ìì
ì í¸ë¦¬íê² ì²ë¦¬íë©´ì, ì ëë ì´í° í¨ìì ì§ì° ì¤í í¹ì±ì íì©í ì ììµëë¤.
ë¹ë기 ì ëë ì´í°ìì íë¡ë¯¸ì¤ê° yieldëë©´, ì´í°ë ì´í° ê²°ê³¼ íë¡ë¯¸ì¤ì ìµì¢ ìíë yieldë íë¡ë¯¸ì¤ì ìíì ì¼ì¹íê² ë©ëë¤.
ì를 ë¤ì´
async function* foo() {
yield Promise.reject(new Error("failed"));
}
foo()
.next()
.catch((e) => console.error(e));
yieldë íë¡ë¯¸ì¤ê° ê±°ë¶ëë©´ ì´í°ë ì´í° ê²°ê³¼ ëí ê±°ë¶ë기 ë문ì, Error: failedê° ë¡ê·¸ë ê²ì
ëë¤. ë¹ë기 ì ëë ì´í° ì´í ê²°ê³¼ì value ìì±ì ë ë¤ë¥¸ íë¡ë¯¸ì¤ê° ìëëë¤.
async function* ì ì¸ì function ì ì¸ê³¼ ë¹ì·íê² ëìí©ëë¤ â ë²ìì ìµìë¨ì¼ë¡ í¸ì´ì¤í
ëì´ ë²ì ë´ ì´ëìë í¸ì¶í ì ìì¼ë©°, í¹ì ë§¥ë½ììë§ ë¤ì ì ì¸í ì ììµëë¤.
ìì
>ë¹ë기 ì ëë ì´í° í¨ì ì ì¸
ë¹ë기 ì ëë ì´í° í¨ìë ê° yield ë¨ê³ê° ë기ì ì´ëë¼ë íì ê²°ê³¼ íë¡ë¯¸ì¤ë¥¼ ìì±í©ëë¤.
async function* myGenerator(step) {
await new Promise((resolve) => setTimeout(resolve, 10));
yield 0;
yield step;
yield step * 2;
}
const gen = myGenerator(2);
gen
.next()
.then((res) => {
console.log(res); // { value: 0, done: false }
return gen.next();
})
.then((res) => {
console.log(res); // { value: 2, done: false }
return gen.next();
})
.then((res) => {
console.log(res); // { value: 4, done: false }
return gen.next();
})
.then((res) => {
console.log(res); // { value: undefined, done: true }
return gen.next();
});
ë¹ë기 ì ëë ì´í° í¨ì를 ì¬ì©íì¬ ì¼ë ¨ì íì¼ ì½ê¸°
ì´ ìì ììë Nodeì fs/promises 모ëì ì¬ì©íì¬, ì¼ë ¨ì íì¼ë¤ì ì½ê³ ìì²ì´ ìì ëë§ ê·¸ ë´ì©ì ì ê·¼í©ëë¤.
async function* readFiles(directory) {
const files = await fs.readdir(directory);
for (const file of files) {
const stats = await fs.stat(file);
if (stats.isFile()) {
yield {
name: file,
content: await fs.readFile(file, "utf8"),
};
}
}
}
const files = readFiles(".");
console.log((await files.next()).value);
// Possible output: { name: 'file1.txt', content: '...' }
console.log((await files.next()).value);
// Possible output: { name: 'file2.txt', content: '...' }
ëª ì¸ì
| Specification |
|---|
| ECMAScript® 2026 Language Specification > # sec-async-generator-function-definitions > |