yield*
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2016ë 9ì.
yield* ííìì ë¤ë¥¸ generator ëë ì´í°ë¬ë¸(iterable) ê°ì²´ì yield를 ììí ë ì¬ì©ë©ëë¤.
ìëí´ ë³´ê¸°
function* func1() {
yield 42;
}
function* func2() {
yield* func1();
}
const iterator = func2();
console.log(iterator.next().value);
// Expected output: 42
구문
yield* [[expression]];
expression-
ì´í°ë¬ë¸(iterable) ê°ì²´ë¥¼ ë°ííë ííì.
ì¤ëª
yield* ííì í¼ì°ì°ì를 ë°ë³µíê³ ë°íëë ê°ì yieldí©ëë¤.
yield* íí ìì²´ì ê°ì ë°ë³µì(iterator)ê° ì¢ ë£ë ë ë°íëë ê°ì ëë¤. (i.e., doneì´ trueì¼ ë)
ìì
>ë¤ë¥¸ ìì±ê¸°(generator)ì ììí기
ë¤ì ì½ëë, next() í¸ì¶ì íµí´ g1()ì¼ë¡ë¶í° yield ëë ê°ì g2()ìì yield ëë ê²ì²ë¼ ë§ëëë¤.
function* g1() {
yield 2;
yield 3;
yield 4;
}
function* g2() {
yield 1;
yield* g1();
yield 5;
}
var iterator = g2();
console.log(iterator.next()); // { value: 1, done: false }
console.log(iterator.next()); // { value: 2, done: false }
console.log(iterator.next()); // { value: 3, done: false }
console.log(iterator.next()); // { value: 4, done: false }
console.log(iterator.next()); // { value: 5, done: false }
console.log(iterator.next()); // { value: undefined, done: true }
ë¤ë¥¸ ì´í°ë¬ë¸(iterable) ê°ì²´
ìì±ê¸° ê°ì²´ ë§ê³ ë, yield*ë ë¤ë¥¸ ë°ë³µ ê°ë¥í ê°ì²´ë yield í ì ììµëë¤. e.g. ë°°ì´, 문ìì´ ëë arguments ê°ì²´
function* g3() {
yield* [1, 2];
yield* "34";
yield* Array.from(arguments);
}
var iterator = g3(5, 6);
console.log(iterator.next()); // { value: 1, done: false }
console.log(iterator.next()); // { value: 2, done: false }
console.log(iterator.next()); // { value: "3", done: false }
console.log(iterator.next()); // { value: "4", done: false }
console.log(iterator.next()); // { value: 5, done: false }
console.log(iterator.next()); // { value: 6, done: false }
console.log(iterator.next()); // { value: undefined, done: true }
yield* íí ìì²´ì ê°
yield* ë êµ¬ë¬¸ì´ ìë ííì
ëë¤. ë°ë¼ì ê°ì¼ë¡ íê°ë©ëë¤.
function* g4() {
yield* [1, 2, 3];
return "foo";
}
var result;
function* g5() {
result = yield* g4();
}
var iterator = g5();
console.log(iterator.next()); // { value: 1, done: false }
console.log(iterator.next()); // { value: 2, done: false }
console.log(iterator.next()); // { value: 3, done: false }
console.log(iterator.next()); // { value: undefined, done: true },
// g4() ë ì¬ê¸°ì { value: "foo", done: true }를 ë°íí©ëë¤
console.log(result); // "foo"
ëª ì¸ì
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-generator-function-definitions-runtime-semantics-evaluation> |