return()
메소드는 제공된 값을 반환하고 Generator를 종료시킨다.
문법
gen.return(value)
매개 변수
value
- 반환될 값.
반환 값
이 함수의 호출과 함께 주어진 인수 값을 반환한다.
예시
return()
사용
아래의 예시는 간단한 Generator와 return
메소드를 보여준다.
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 }
참고사항:
만약 done
이 true
이면 반환되는 객체의 value
프로퍼티의 값은 undefined
이다. (return
(값)은 next
()와 동일)
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 }
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Generator.prototype.return' in that specification. |
Standard | Initial definition. |
ECMAScript 2017 Draft (ECMA-262) The definition of 'Generator.prototype.return' in that specification. |
Draft |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | ? | 38 (38) | ? | ? | ? |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | ? | ? | 38.0 (38) | ? | ? | ? |