function*
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ì.
function* ì ì¸ (ëì ë³íê° ìë function keyword) ì generator function ì ì ìíëë°, ì´ í¨ìë Generator ê°ì²´ë¥¼ ë°íí©ëë¤.
ìëí´ ë³´ê¸°
function* generator(i) {
yield i;
yield i + 10;
}
const gen = generator(10);
console.log(gen.next().value);
// Expected output: 10
console.log(gen.next().value);
// Expected output: 20
generator function ì GeneratorFunction ìì±ìì function* expression ì ì¬ì©í´ì ì ìí ì ììµëë¤.
문ë²
function* name([param[, param[, ... param]]]) {
statements
}
name-
í¨ìëª .
param-
í¨ìì ì ë¬ëë ì¸ìì ì´ë¦. í¨ìë ì¸ì를 255ê°ê¹ì§ ê°ì§ ì ìë¤.
statements-
í¨ìì 본체를 구ì±íë 구문ë¤.
ì¤ëª
Generatorë ë¹ ì ¸ëê°ë¤ê° ëì¤ì ë¤ì ëìì¬ ì ìë í¨ìì ëë¤. ì´ë 컨í ì¤í¸(ë³ì ê°)ë ì¶ì ê³¼ì ìì ì ì¥ë ìíë¡ ë¨ì ììµëë¤.
Generator í¨ìë í¸ì¶ëì´ë ì¦ì ì¤íëì§ ìê³ , ëì í¨ì를 ìí Iterator ê°ì²´ê° ë°íë©ëë¤. Iteratorì next() ë©ìë를 í¸ì¶íë©´ Generator í¨ìê° ì¤íëì´ yield 문ì ë§ë ëê¹ì§ ì§ííê³ , í´ë¹ ííìì´ ëª
ìíë Iteratorë¡ë¶í°ì ë°íê°ì ë°íí©ëë¤. yield* ííìì ë§ì£¼ì¹ ê²½ì°, ë¤ë¥¸ Generator í¨ìê° ìì(delegate)ëì´ ì§íë©ëë¤.
ì´í next() ë©ìëê° í¸ì¶ëë©´ ì§íì´ ë©ì·ë ìì¹ììë¶í° ì¬ì¤íí©ëë¤. next() ê° ë°ííë ê°ì²´ë yieldë¬¸ì´ ë°íí ê°(yielded value)ì ëíë´ë value ìì±ê³¼, Generator í¨ì ìì 모ë yield 문ì ì¤í ì¬ë¶ë¥¼ íìíë boolean íì
ì done ìì±ì ê°ìµëë¤. next() 를 ì¸ìê°ê³¼ í¨ê» í¸ì¶í ê²½ì°, ì§íì ë©ì·ë ìì¹ì yield 문ì next() ë©ìëìì ë°ì ì¸ìê°ì¼ë¡ ì¹ííê³ ê·¸ ìì¹ìì ë¤ì ì¤ííê² ë©ëë¤.
ìì
>ê°ë¨í ìì
function* idMaker() {
var index = 0;
while (index < 3) yield index++;
}
var gen = idMaker();
console.log(gen.next().value); // 0
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
console.log(gen.next().value); // undefined
// ...
yield* 를 ì¬ì©í ìì
function* anotherGenerator(i) {
yield i + 1;
yield i + 2;
yield i + 3;
}
function* generator(i) {
yield i;
yield* anotherGenerator(i);
yield i + 10;
}
var gen = generator(10);
console.log(gen.next().value); // 10
console.log(gen.next().value); // 11
console.log(gen.next().value); // 12
console.log(gen.next().value); // 13
console.log(gen.next().value); // 20
Generator ì ì¸ìê°ì ë기ë ìì
function* logGenerator() {
console.log(yield);
console.log(yield);
console.log(yield);
}
var gen = logGenerator();
// the first call of next executes from the start of the function
// until the first yield statement
gen.next();
gen.next("pretzel"); // pretzel
gen.next("california"); // california
gen.next("mayonnaise"); // mayonnaise
Generator ë ìì±ìë¡ì ì¬ì©ë ì ìë¤
function* f() {}
var obj = new f(); // throws "TypeError: f is not a constructor"
ëª ì¸ì
| Specification |
|---|
| ECMAScript® 2027 Language Specification > # sec-generator-function-definitions > |
ë¸ë¼ì°ì í¸íì±
ê´ë ¨ í목
function* expressionGeneratorFunctionobject- The Iterator protocol
yieldyield*Functionobjectfunction declarationfunction expressionFunctions and function scope- Other web resources:
- Regenerator an ES2015 generator compiler to ES5
- Forbes Lindesay: Promises and Generators: control flow utopia â JSConf EU 2013
- Hemanth.HM: The New gen of *gen(){}
- Task.js