-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generator #30
Comments
Generator
Generator ํจ์์ ์ ์
// ํจ์ ์ ์ธ๋ฌธ
function* decFunc() {
yield 1;
}
let genObj = decFunc();
// ํจ์ ํํ์
const expFunc = function* () {
yield 1;
};
genObj = expFunc();
// ๋ฉ์๋
const obj = {
* objectMethod() {
yield 1;
}
};
genObj = obj.objectMethod();
// ํด๋์ค ๋ฉ์๋
class GenClass {
* classMethod() {
yield 1;
}
}
const genClass = new GenClass();
genObj = genClass.classMethod(); Generator ๊ฐ์ฒด
function* counter() {
console.log('First');
yield 1;
console.log('Second');
yield 2;
console.log('Third');
yield 3;
console.log('The end');
}
const genObj = counter();
console.log(genObj.next()) //{value: 1, done: false}
console.log(genObj.next()) //{value: 2, done: false}
console.log(genObj.next()) //{value: 3, done: false}
console.log(genObj.next()) //{value: undefined, done: true}
Generator ๊ฐ์ฒด๋ฅผ ์ด์ฉํ ์ดํฐ๋ฌ๋ธ ๊ตฌํconst genObj = (function* () {
let i = 0;
while(true) {
yield ++i;
}
}());
for (let item of genObj) {
if (item === 10) break;
console.log(item);
} // Generator ํจ์์ ํ๋ผ๋ฏธํฐ ์ ๋ฌ
const genObj = function* (max) {
let i = 0;
while(true) {
if (i === max) break;
yield ++i;
}
}
for (let item of genObj(10)) {
console.log(item);
} // next ๋ฉ์๋์ ํ๋ผ๋ฏธํฐ ์ ๋ฌ
function* genFunc(n) {
let res;
res = yield n;
console.log(res);
res = yield res;
console.log(res);
res = yield res;
console.log(res);
return res;
}
const genObj = genFunc(0);
console.log(genObj.next());
console.log(genObj.next(1));
console.log(genObj.next(2));
console.log(genObj.next(3)); Generator๋ฅผ ์ด์ฉํ ๋น๋๊ธฐ ์ฒ๋ฆฌ
function getId(phoneNumber) {
// โฆ
iterator.next(result);
}
function getEmail(id) {
// โฆ
iterator.next(result);
}
function getName(email) {
// โฆ
iterator.next(result);
}
function order(name, menu) {
// โฆ
iterator.next(result);
}
function* orderCoffee(phoneNumber) {
const id = yield getId(phoneNumber);
const email = yield getEmail(id);
const name = yield getName(email);
const result = yield order(name, 'coffee');
return result;
}
const iterator = orderCoffee('010-1234-1234');
iterator.next(); Generator๋ ์ด๋ป๊ฒ ๊ตฌํ๋์ด ์์๊น?// ES6
function* foo(){
yield bar();
}
// ES5 Compiled
"use strict";
var _marked = /*#__PURE__*/ regeneratorRuntime.mark(foo);
function foo() {
return regeneratorRuntime.wrap(
function foo$(_context) {
while (1) {
switch ((_context.prev = _context.next)) {
case 0:
_context.next = 2;
return bar();
case 2:
case "end":
return _context.stop();
}
}
},
_marked, this
);
}
๋์
์ฝ๋์ ์ญ์ฌ๋ฅผ ๋ฐ๋ผ๊ฐ๋ค ๋ณด๋ฉด facebook/regenerator repository์ ๋๋ฌํ๊ฒ ๋๋ค. ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ 2013๋ Node.js v0.11.2์์ generator syntax๋ฅผ ์ง์ํ๊ธฐ ์ํด ๋ง๋ค์ด ์ก์ผ๋ฉฐ, Babel์์๋ ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ generator๋ฅผ ๊ตฌํํ๊ณ ์๋ค. ์ค์ ์ฝ๋๋ฅผ ๋ค์ฌ๋ค๋ณด๋ฉด Symbol๊ณผ Iterator๋ฅผ ์ด์ฉํด์ Iterable Protocol์ ๊ตฌํํ๊ณ ์๋ค. |
๐ Reference
The text was updated successfully, but these errors were encountered: