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

Generator.prototype.return()

The return() method returns the given value and finishes the generator.

Syntax

gen.return(value)

Parameters

value
The value to return.

Return value

The value that is given as an argument.

Examples

Using return()

The following example shows a simple generator and the return method.

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 }

Note:

if done is true,  return(value) as same as next(),value is 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 }

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) ? ? ?

See also

Document Tags and Contributors

 Contributors to this page: eduardoboucas, panhezeng, fscholz
 Last updated by: eduardoboucas,