Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

yield

현재 번역은 완벽하지 않습니다. 한국어로 문서 번역에 동참해주세요.

yield 키워드는 생성기 함수 (function* 또는 오래된 생성기 함수)를 중지하거나 재개하는데 사용됩니다.

문법

[rv] = yield [expression];
expression
생성기 함수에서 반복자 프로토콜을 통해 반환할 값을 정의합니다.  값이 생략되면  undefined를 반환합니다.
rv

생성기 실행을 계속 이어가기 위해서, 생성기의 next() 메서드에게 전달된 선택값을 반환합니다.

설명

yield는 생성기 함수 실행을 일시 중지하고, yield 키워드 뒤에오는 표현의 값을 생성기의 콜러(caller)에게 반환합니다. return 키워드의 생성기 버전이라 보면 됩니다.  

yield 키워드는 실제로 두 개의 속성을 가진 IteratorResult 객체를 반환합니다. value 속성은 yield 표현(expression)의 실행 결과를 보여주고, done 속성은 생성기 함수가 완전히 종료되었는지 여부를 불린(Boolean) 형태로 보여줍니다. 

Once paused on a yield expression, the generator's code execution remains paused until the generator's next() method is called. Each time the generator's next() method is called, the generator resumes execution and runs until it reaches one of the following:

  •  A yield, which causes the generator to once again pause and return the generator's new value. The next time next() is called, execution resumes with the statement immediately after the yield.
  • throw is used to throw an exception from the generator. This halts execution of the generator entirely, and execution resumes in the caller as is normally the case when an exception is thrown.
  • The end of the generator function is reached; in this case, execution of the generator ends and an IteratorResult is returned to the caller in which the value is undefined and done is true.
  • A return statement is reached. In this case, execution of the generator ends and an IteratorResult is returned to the caller in which the value is the value specified by the return statement and done is true.

If an optional value is passed to the generator's next() method, that value becomes the value returned by the generator's current yield operation.

Between the generator's code path, its yield operators, and the ability to specify a new starting value by passing it to Generator.prototype.next(), generators offer enormous power and control.

예시

The following code is the declaration of an example generator function, along with a helper function.

function* foo(){
  var index = 0;
  while (index <= 2) // when index reaches 3, 
                     // yield's done will be true 
                     // and its value will be undefined;
    yield index++;
}

Once a generator function is defined, it can be used by constructing an iterator as shown.

var iterator = foo();
console.log(iterator.next()); // { value: 0, done: false }
console.log(iterator.next()); // { value: 1, done: false }
console.log(iterator.next()); // { value: 2, done: false }
console.log(iterator.next()); // { value: undefined, done: true }

Specifications

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Yield' in that specification.
Standard Initial definition.
ECMAScript 2017 Draft (ECMA-262)
The definition of 'Yield' in that specification.
Draft  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 39 26.0 (26.0) ? ? ?
IteratorResult object instead of throwing ? 29.0 (29.0) ? ? ?
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) 26.0 (26.0) ? ? ?
IteratorResult object instead of throwing ? 29.0 (29.0) ? ? ?

Firefox-specific notes

  • Starting with Gecko 29 (Firefox 29 / Thunderbird 29 / SeaMonkey 2.26), the completed generator function no longer throws a TypeError "generator has already finished". Instead, it returns an IteratorResult object like { value: undefined, done: true } (bug 958951).
  • Starting with Gecko 33 (Firefox 33 / Thunderbird 33 / SeaMonkey 2.30), the parsing of the yield expression has been updated to conform with the latest ES6 specification (bug 981599):
    • The expression after the yield keyword is optional and omitting it no longer throws a SyntaxError: function* foo() { yield; }

See also

문서 태그 및 공헌자

태그: 
 이 페이지의 공헌자: Yunhong-Min, preco21
 최종 변경: Yunhong-Min,