JS Generator Function

Creator
Creator
Seonglae ChoSeonglae Cho
Created
Created
2021 Mar 23 2:14
Editor
Edited
Edited
2023 Nov 16 6:33
Refs
Refs

generator function

Generator 객체를 반환합니다.
function* generator(i) { yield i; yield i + 10; }
 
*[Symbol.iterator]() { // [Symbol.iterator]: function*()를 짧게 줄임
 
const iterable1 = {}; iterable1[Symbol.iterator] = function* () { yield 1; yield 2; yield 3; }; console.log([...iterable1]);
 
 
const iterable1 = {}; iterable1[Symbol.asyncIterator] = async function* () { yield 1; yield 2; yield 3; }; (async () => { for await (const x of iterable1 ) { console.log(x); // expected output: // "hello" // "async" // "iteration!" } })();
 
 
function*
function* 선언 (끝에 별표가 있는 function keyword) 은 generator function 을 정의하는데, 이 함수는 객체를 반환합니다. generator function 은 GeneratorFunction 생성자와 function* expression 을 사용해서 정의할 수 있습니다. 함수에 전달되는 인수의 이름. 함수는 인수를 255개까지 가질 수 있다. Generator는 빠져나갔다가 나중에 다시 돌아올 수 있는 함수입니다.
function*
function*
The function* declaration ( function keyword followed by an asterisk) defines a generator function, which returns a object. You can also define generator functions using the GeneratorFunction constructor, or the function expression syntax. Generators are functions that can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances.
 
 

Recommendations