[JS] Generator

강은비·2022λ…„ 1μ›” 3일
0

JS

λͺ©λ‘ 보기
11/19
post-thumbnail
post-custom-banner

πŸ“Œ μ œλ„ˆλ ˆμ΄ν„° ν•¨μˆ˜

  • μ›ν•˜λŠ” 만큼 μ½”λ“œ 싀행을 μ‹œμž‘ν•˜κ±°λ‚˜ 쀑지할 수 μžˆλŠ” ν•¨μˆ˜μ΄λ‹€.
  • Genarator 객체λ₯Ό λ°˜ν™˜ν•œλ‹€.
function* fruitList(){
    yield "Banana";
    yield "Apple";
    yield "Orange";
}
  • function*을 μ‚¬μš©ν•˜μ—¬ ν•¨μˆ˜λ₯Ό 선언함.
  • λ°˜ν™˜ν•  κ°’ μ•žμ— yield ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•¨.
  • λ©”μ„œλ“œ
    • Generator.prototype.next()
    • Generator.prototype.return()
    • Generator.prototype.throw()

πŸ“Œ μ œλ„ˆλ ˆμ΄ν„° μ‹€ν–‰ν•˜κΈ°

gen.next(value)
value: generator function에 전달할 κ°’

  • next() λ©”μ„œλ“œκ°€ 호좜되면 μ œλ„ˆλ ˆμ΄ν„° ν•¨μˆ˜κ°€ yield 문을 λ§Œλ‚˜κΈ° μ „κΉŒμ§€ μ‹€ν–‰λœλ‹€.
  • 또 λ‹€μ‹œ λ©”μ„œλ“œκ°€ 호좜되면 진행이 λ©ˆμ·„λ˜ μœ„μΉ˜μ—μ„œλΆ€ν„° μž¬μ‹€ν–‰ν•œλ‹€.
  • value와 done ν”„λ‘œνΌν‹°λ₯Ό 가진 객체λ₯Ό λ°˜ν™˜ν•œλ‹€.
    • value ν”„λ‘œνΌν‹°λŠ” yield문이 λ°˜ν™˜ν•œ κ°’(yielded value)을 κ°–λŠ”λ‹€.
    • done ν”„λ‘œνΌν‹°λŠ” λͺ¨λ“  yield 문의 μ‹€ν–‰ μ—¬λΆ€λ₯Ό λ‚˜νƒ€λ‚΄λ©° true ν˜Ήμ€ false 값을 κ°–λŠ”λ‹€.
    • done이 true일 경우 valueλŠ” undefined 값을 κ°–λŠ”λ‹€.
  • next() λ₯Ό μΈμžκ°’κ³Ό ν•¨κ»˜ ν˜ΈμΆœν•  경우, 진행을 λ©ˆμ·„λ˜ μœ„μΉ˜μ˜ yield 문을 μΈμžκ°’μœΌλ‘œ μΉ˜ν™˜ν•˜κ³  κ·Έ μœ„μΉ˜μ—μ„œ λ‹€μ‹œ μ‹€ν–‰ν•˜κ²Œ λœλ‹€.

μ˜ˆμ‹œ

function* fruitList(){
	yield "Banana";
    yield "Apple";
    yield "Orange";
}
const fruits = fruitList();

fruits.next();    // {value: "Banana", done: false}
fruits.next();    // {value: "Apple", done: false}
fruits.next();    // {value: "Orange", done: false}
fruits.next();    // {value: "undefined", done: true}
  • 첫 번째 next() λ©”μ„œλ“œμ— 인자λ₯Ό λ„£μœΌλ©΄ λ¬΄μ‹œλ¨.
function* gen() {
  while(true) {
    var value = yield null;
    console.log(value);
  }
}

var g = gen();
g.next(1);
// "{ value: null, done: false }"
g.next(2);
// "{ value: null, done: false }"
// 2

πŸ“Œ μ œλ„ˆλ ˆμ΄ν„° μ’…λ£Œν•˜κΈ°

gen.return(value)
value: λ°˜ν™˜λ  κ°’

  • return() λ©”μ„œλ“œλŠ” μ œλ„ˆλ ˆμ΄ν„°λ₯Ό μ’…λ£Œν•œλ‹€.
  • done: trueλ₯Ό λ°˜ν™˜ν•˜κ³  인자 값이 μžˆλ‹€λ©΄ value에 κ·Έ 값을 λ°˜ν™˜ν•œλ‹€.
function* gen() {
  yield 1;
  yield 2;
  yield 3;
}

var g = gen();

g.next();        // { value: 1, done: false }
g.return("foo"); // { value: "foo", done: true }
g.next();        // { value: undefined, done: true }
  • yieldν•  컨텐츠λ₯Ό 이미 λ‹€ λ°˜ν™˜ν•˜μ—¬ μ œλ„ˆλ ˆμ΄ν„° μƒνƒœ(done)κ°€ true라면 객체의 value ν”„λ‘œνΌν‹° 값은 계속 undefined이닀.
function* gen() {yield 1};
var g = gen();
console.log(g.next());    //{ value: 1, done: false }
console.log(g.next());    //{ value: undefined, done: true }
console.log(g.return(1)); //{ value: undefined, done: true }

πŸ“Œ μ œλ„ˆλ ˆμ΄ν„°λ‘œ μ—λŸ¬ 생성

gen.throw(exception)

  • Generator ν•¨μˆ˜μ— errorλ₯Ό λ°œμƒμ‹œν‚€κ³  κ°•μ œμ’…λ£Œμ‹œν‚¨λ‹€.
  • {value: undefined, done: true} λ°˜ν™˜
  • Generator ν•¨μˆ˜μ˜ catch()λ¬Έμ—μ„œ errorλ₯Ό 받을 수 있음
function* gen(){
    try{
    	yield "Trying...";
        yield "Trying harder...";
        yield "Trying even harder...";
    }
    catch(err){
    	console.log("Error: " + err);
    }
}

const myGenrator = gen();
myGenerator.next();		// {value: "Trying...", done: false}
myGenerator.next();		// {value: "Trying harder...", done: false}
myGenerator.throw("opps");
// Error: opps
// {value: undefined, done: true}

πŸ“Œ μ œλ„ˆλ ˆμ΄ν„°μ™€ ν”„λ‘œλ―ΈμŠ€ 같이 μ‚¬μš©ν•˜κΈ°

const myPromise= () => new Promise((resolve) =>{
    resolve("our value is...");
});

function* gen() {
    let result = "";
   yield myPromise().then(data => { result = data});
   yield result + ' 2';
};

const asyncFunc = gen();
const val1 = asyncFunc.next();
console.log(val1);		// {value: Promise, done: false}
val1.value.then(() => {
    console.log(asyncFunc.next());
});
// {value: "our value is ...2", done: false}

μ°Έκ³ 

post-custom-banner

0개의 λŒ“κΈ€