๐Ÿ“– [JavaScript] Generator

ํ˜ฑยท2022๋…„ 9์›” 10์ผ

JavaScript_Intermediate

๋ชฉ๋ก ๋ณด๊ธฐ
19/19

๐Ÿ”Ž Generator

ํ•จ์ˆ˜์˜ ์‹คํ–‰์„ ์ค‘๊ฐ„์— ๋ฉˆ์ท„๋‹ค๊ฐ€ ์ œ๊ฐœํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ
ํ•จ์ˆ˜ ์˜†์— *๋ฅผ ๋„ฃ์–ด์„œ ๋งŒ๋“ค๊ณ ,
๋‚ด๋ถ€์— yield ํ‚ค์›Œ๋“œ๋ฅผ ๋„ฃ์–ด์„œ ์‚ฌ์šฉ
yield์—์„œ ํ•จ์ˆ˜์˜ ์‹คํ–‰์„ ๋ฉˆ์ถœ ์ˆ˜ ์žˆ์Œ

function* fn() {
  yield 1;
  yield 2;
  yield 3;
  return 'finish';
}

const a =fn();

๐Ÿ“Œ next()

Generator๋ฅผ ์‹คํ–‰ํ•˜๋ฉด Generator ๊ฐ์ฒด๊ฐ€ ๋ฐ˜ํ™˜
Generator ๊ฐ์ฒด๋Š” next method๊ฐ€ ์žˆ์Œ
์ฆ‰, ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋ ค๋ฉด next()๊ฐ€ ํ•„์š”

function* fn() {
  console.log(1);
  yield 1;
  console.log(2);
  yield 2;
  console.log(3);
  console.log(4);
  yield 3;
  return 'finish';
}

const a =fn();
a.next(); //1 ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด yield ๋ฌธ์„ ๋งŒ๋‚  ๋•Œ๊นŒ์ง€ ์‹คํ–‰๋˜๊ณ  ๋ฐ˜ํ™˜
//{value:1, done: false}

๋ฐ˜ํ™˜๋œ data๊ฐ์ฒด๋Š” value์™€ done property๋ฅผ ๊ฐ€์ง

๐Ÿ“Œ value, done

value: yield ์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” ๊ฐ’, ์—†์œผ๋ฉด undefined
done: ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ํ•จ์ˆ˜๊ฐ€ ๋๋‚ฌ๋Š” ์ง€ ๋‚˜ํƒ€๋ƒ„ (๋๋‚จ: true, ์•„๋‹˜: false)

function* fn() {
  console.log(1);
  yield 1;
  console.log(2);
  yield 2;
  console.log(3);
  console.log(4);
  yield 3;
  return 'finish';
}

const a =fn();
a.next(); 
//{value:1, done: false}
...(๊ณ„์† ์‹คํ–‰)
a.next(); 
//{value: 'finish', done: true}
a.next(); 
//{value: undefined , done: true}

๐Ÿ“Œ return(), throw()

Generator๋Š” next method ์™ธ์—, return๊ณผ throw method๊ฐ€ ์žˆ์Œ

โœ”๏ธ return()

๊ทธ ์ฆ‰์‹œ done ๊ฐ’์ด true
์ดํ›„์— next๋ฅผ ์‹คํ–‰ํ•ด๋„ value๋ฅผ ์–ป์–ด์˜ฌ ์ˆ˜ ์—†๊ณ  done์€ true์ž„

function* fn() {
  console.log(1);
  yield 1;
  console.log(2);
  yield 2;
  console.log(3);
  console.log(4);
  yield 3;
  return 'finish';
}

const a =fn();
a.return('END'); 
//{value:'END', done: true }
a.next();
//{value: undefined , done: true}

โœ”๏ธ throw()

done์„ true๋ฅผ ๋ฐ”๊ฟˆ

function* fn() {
  try{
    console.log(1);
  	yield 1;
  	console.log(2);
  	yield 2;
  	console.log(3);
  	console.log(4);
  	yield 3;
  	return 'finish';
  } catch (e) {
    console.log(e);
  }
}

const a =fn();
a.throw(new Error('err'))
//err, {value: undefined , done: true}

๐Ÿ“Œ ์†์„ฑ

โœ”๏ธ iterable: ๋ฐ˜๋ณต ๊ฐ€๋Šฅ

โ˜‘๏ธ ์กฐ๊ฑด

Symbol.iterator ๋ฉ”์„œ๋“œ๊ฐ€ ๊ตฌํ˜„๋˜์–ด์•ผ ํ•จ.
๋งค์„œ๋“œ ํ˜ธ์ถœ์˜ ๊ฒฐ๊ณผ ์ฆ‰, Symbol.iterator๋Š” iterator๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค

โ˜‘๏ธ iterator

value, done property๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋งค์†Œ๋“œ next๊ฐ€ ์žˆ์–ด์•ผ ํ•จ

์ฆ‰, Generator=iterator=iterable
๋ฐฐ์—ด ๋˜ํ•œ Symbol.iterator๊ฐ€ ์žˆ๊ณ , next๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ๋ฐ˜๋ณต ๊ฐ€๋Šฅ!!!!
๋ฐฐ์—ด= ๋ฐ˜๋ณต๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด

function* fn() {
  yield 1;
  yield 2;
  yield 3;
}

const a =fn();

a[Symbol.iterator]() ===a;
//true

Generator์— Symbol.iterator ์‹คํ–‰ ๊ฐ’ == ์ž๊ธฐ ์ž์‹ 
Generator๋Š” iterable ๊ฐ์ฒด
๋”ฐ๋ผ์„œ for of ๋ฅผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

+๋ฌธ์ž์—ด๋„ iterable์ž„

โœ”๏ธ next() ์ธ์ˆ˜ ์ „๋‹ฌ

function* fn() {
  const num1 = yield '์ฒซ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.';
  console.log(num1);
  
  const num2 = yield '๋‘๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.';
  console.log(num2);
  
  return num1+num2
}

const a =fn();

a.next()
//{value: '์ฒซ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.', done: false}
a.next(2);
//2
//{value: ๋‘๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.', done: false}
a.next(4);
//4
//{value: 6, done: true} ๋‘ ์ˆซ์ž๋ฅผ ๋”ํ•œ return ๊ฐ’์ด ๋‚˜์˜ด

โœ”๏ธ Generator ๊ฐ’

Generator๋Š” ๊ฐ’์„ ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด ๋‘์ง€ ์•Š์Œ
๋”ฐ๋ผ์„œ while(true)๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฌดํ•œ ๋ฐ˜๋ณต์„ํ•ด๋„ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๋ป—์ง€ ์•Š์Œ
ํ•„์š”ํ•œ ๋ถ€๋ถ„๋งŒ ์—ฐ์‚ฐํ•ด์„œ ๊ฐ’์„ ์คŒ

function* fn() {
  let index = 0;
  while (true) {
    yield index++;
  }
}

const a =fn();
a.next();
...~

Generator๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๋ฌดํ•œ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋จ
Generator ํ•„์š”ํ•œ ๊ฐ’๋งŒ ๊ทธ๋•Œ๊ทธ๋•Œ ์ƒ์„ฑํ•จ.
ํ•„์š”ํ•œ ์ˆœ๊ฐ„๊นŒ์ง€ ๊ณ„์‚ฐ์„ ๋ฏธ๋ฃฐ ์ˆ˜ ์žˆ์Œ

โœ”๏ธ yield* ์‚ฌ์šฉ

๋‹ค๋ฅธ Generator ๋ถˆ๋Ÿฌ ์˜ค๊ธฐ
๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ ์˜ฌ ์ˆ˜ ์žˆ์Œ

function* gen1() {
  yield 'W';
  yield 'o';
  yield 'r';
  yield 'l';
  yield 'd';
}

funciton* gen2(){
  yield 'Hello';
  yield* gen1(); //๋‹ค๋ฅธ Generator ํ•จ์ˆ˜๋ฅผ ๋ถˆ๋Ÿฌ์˜ด, ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฐ์ฒด ๊ฐ€๋Šฅ!
  yield '!';
}

console.log(...gen2())
Hello, W o r l d ! 

๐Ÿ“Œ ์ •๋ฆฌ

Generator๋Š” ๋‹ค๋ฅธ ์ž‘์—…์„ ํ•˜๋‹ค๊ฐ€ ๋‹ค์‹œ ๋Œ์•„์™€์„œ next()ํ•ด์ฃผ๋ฉด
์ง„ํ–‰์ด ๋ฉˆ์ท„๋˜ ๋ถ€๋ถ„๋ถ€ํ„ฐ ์ด์–ด์„œ ์‹คํ–‰

profile
new blog: https://hae0-02ni.tistory.com/

0๊ฐœ์˜ ๋Œ“๊ธ€