Apply your JS skills to key Mozilla projects as an MDN Fellow! http://mzl.la/MDNFellowship

mozilla
Your Search Results

    function*

    This is an experimental technology, part of the ECMAScript 6 (Harmony) proposal.
    Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.

    Summary

    The function* declaration (function keyword followed by an asterisk) defines a generator function, which returns a Generator object.

    You can also define generator functions using the GeneratorFunction constructor and a function* expression.

    Syntax

    function* name([param[, param[, ... param]]]) {
       statements
    }
    
    name
    The function name.
    param
    The name of an argument to be passed to the function. A function can have up to 255 arguments.
    statements
    The statements comprising the body of the function.

    Description

    Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances.

    Calling a generator function does not execute its body immediately; an iterator object for the function is returned instead. When the iterator's next() method is called, the generator function's body is executed until the first yield expression, which specifies the value to be returned from the iterator or, with yield*, delegates to another generator function. The next() method returns an object with a value property containing the yielded value and a done property which indicates whether the generator has yielded its last value.

    Examples

    Simple example

    function* idMaker(){
        var index = 0;
        while(true)
            yield index++;
    }
    
    var gen = idMaker();
    
    console.log(gen.next().value); // 0
    console.log(gen.next().value); // 1
    console.log(gen.next().value); // 2
    // ...

    Example with yield*

    function* anotherGenerator(i) {
      yield i + 1;
      yield i + 2;
      yield i + 3;
    }
    function* generator(i){
      yield i;
      yield* anotherGenerator(i);
      yield i + 10;
    }
    
    var gen = generator(10);
    
    console.log(gen.next().value); // 10
    console.log(gen.next().value); // 11
    console.log(gen.next().value); // 12
    console.log(gen.next().value); // 13
    console.log(gen.next().value); // 20
    

    Specifications

    Specification Status Comment
    ECMAScript 6 (ECMA-262)
    The definition of 'function*' in that specification.
    Release Candidate Initial definition.

    Browser compatibility

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

    Firefox-specific notes

    Generators and iterators in Firefox versions before 26

    Older Firefox versions implement an older version of the generators proposal. In the older version, generators were defined using a regular function keyword (without an asterisk) among other differences.

    IteratorResult object returned instead of throwing

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

    See also

    Document Tags and Contributors

    Last updated by: fscholz,
    Hide Sidebar