• JavaScript
  • JavaScript
  • Tutorials and guides
  • Beginner's tutorials
    1. JavaScript Guide
      1. Loops and iteration
      2. Representing dates & times
      3. Working with objects
      4. Iterators and generators
      5. Asynchronous JavaScript
      6. Equality comparisons and sameness
      7. Meta programming
      8. AggregateError
      9. AsyncGenerator
      10. BigInt
      11. DataView
      12. encodeURI()
      13. escape() Deprecated
      14. Float16Array
      15. Generator
      16. Int8Array
      17. InternalError Non-standard
      18. Iterator
      19. NaN
      20. parseInt()
      21. ReferenceError
      22. SharedArrayBuffer
      23. Temporal Experimental
      24. Uint8ClampedArray
      25. unescape() Deprecated
      26. WeakSet
  • Assignment (=)
  • Bitwise AND (&)
  • Bitwise OR assignment (|=)
  • Comma operator (,)
  • Destructuring
  • Exponentiation (**)
  • Greater than (>)
  • import.meta.resolve()
  • Inequality (!=)
  • Less than (<)
  • Logical NOT (!)
  • Multiplication assignment (*=)
  • Nullish coalescing assignment (??=)
  • Optional chaining (?.)
  • Right shift (>>)
  • Strict inequality (!==)
  • this
  • Unsigned right shift (>>>)
  • yield*
  • Block statement
  • continue
  • export
  • for...in
  • if...else
  • let
  • try...catch
  • with Deprecated
  • get
  • The arguments object
    1. callee Deprecated
    2. extends
    3. Static initialization blocks
  • Character class escape: \d, \D, \w, \W, \s, \S
  • Input boundary assertion: ^, $
  • Modifier: (?ims-ims:...)
  • Quantifier: *, +, ?, {n}, {n,}, {n,m}
  • Errors
    1. RangeError: argument is not a valid code point
    2. RangeError: invalid array length
    3. RangeError: repeat count must be less than infinity
    4. ReferenceError: assignment to undeclared variable "x"
    5. SyntaxError: 'arguments'/'eval' can't be defined or assigned to in strict mode code
    6. SyntaxError: \ at end of pattern
    7. SyntaxError: await is only valid in async functions, async generators and modules
    8. SyntaxError: continue must be inside loop
    9. SyntaxError: function statement requires a name
    10. SyntaxError: identifier starts immediately after numeric literal
    11. SyntaxError: invalid assignment left-hand side
    12. SyntaxError: invalid class set operation in regular expression
    13. SyntaxError: invalid property name in regular expression
    14. SyntaxError: invalid unicode escape in regular expression
    15. SyntaxError: missing ) after argument list
    16. SyntaxError: missing } after property list
    17. SyntaxError: missing variable name
    18. SyntaxError: numbers out of order in {} quantifier.
    19. SyntaxError: property name __proto__ appears more than once in object literal
    20. SyntaxError: rest parameter may not have a default
    21. SyntaxError: super() is only valid in derived class constructors
    22. SyntaxError: unlabeled break must be inside loop or switch
    23. TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed
    24. TypeError: "x" is not a function
    25. TypeError: BigInt value can't be serialized in JSON
    26. TypeError: can't convert BigInt to number
    27. TypeError: can't redefine non-configurable property "x"
    28. TypeError: class constructors must be invoked with 'new'
    29. TypeError: Initializing an object twice is an error with private fields/methods
    30. TypeError: Iterator/AsyncIterator constructor can't be used directly
    31. TypeError: property "x" is non-configurable and can't be deleted
    32. TypeError: X.prototype.y called on incompatible type
    33. JavaScript technologies overview
    34. Strict mode
    35. Learn more
    36. See full compatibility
  • The yield* operator can be used within generator (sync or async) functions to delegate to another AsyncGenerator.

    Try it

    function* func1() {
      yield 42;
    }
    
    function* func2() {
      yield* func1();
    }
    
    const iterator = func2();
    
    console.log(iterator.next().value);
    / Expected output: 42
    

    Syntax

    js
    yield* expression
    

    Parameters

    expression Optional

    An iterable object.

    Return value

    Returns the value returned by that iterator when it's closed (when done is true).

    Description

    The yield* expression iterates over the operand and yields each value returned by it. It delegates iteration of the current generator to an underlying iterator — which we will refer to as "generator" and "iterator", respectively. yield* first gets the iterator from the operand by calling the latter's [Symbol.iterator]() method. Then, each time the next() method of the generator is called, yield* calls the iterator's next() method, passing the argument received by the generator's next() method (always undefined for the first call), and yielding the same result object as what's returned from the iterator's next() method. If the iterator result has done: true, then the yield* expression stops executing and returns the value of that result.

    The yield* operator forwards the current generator's return() methods to the underlying iterator as well. If the current generator is prematurely closed through one of these methods, the underlying iterator will be notified. If the generator's throw()/return() method is called, the throw()/return() method of the underlying iterator is called with the same argument. The return value of throw()/return() is handled like the next() method's result, and if the method throws, the exception is propagated from the yield* expression.

    If the underlying iterator doesn't have a return() method, the yield* expression turns into a yield expression.

    If the underlying iterator doesn't have a throw() method, this causes yield* to throw a TypeError – but before throwing the error, the underlying iterator's return() method is called if one exists.

    Examples

    Delegating to another generator

    In following code, values yielded by g1() are returned from next() calls just like those which are yielded by g2().

    js
    function* g1() {
      yield 2;
      yield 3;
      yield 4;
    }
    
    function* g2() {
      yield 1;
      yield* g1();
      yield 5;
    }
    
    const gen = g2();
    
    console.log(gen.next()); / {value: 1, done: false}
    console.log(gen.next()); / {value: 2, done: false}
    console.log(gen.next()); / {value: 3, done: false}
    console.log(gen.next()); / {value: 4, done: false}
    console.log(gen.next()); / {value: 5, done: false}
    console.log(gen.next()); / {value: undefined, done: true}
    

    Other Iterable objects

    Besides generator objects, yield* can also yield other kinds of iterables (e.g., arrays, strings, or arguments objects).

    js
    function* g3(...args) {
      yield* [1, 2];
      yield* "34";
      yield* args;
    }
    
    const gen = g3(5, 6);
    
    console.log(gen.next()); / {value: 1, done: false}
    console.log(gen.next()); / {value: 2, done: false}
    console.log(gen.next()); / {value: "3", done: false}
    console.log(gen.next()); / {value: "4", done: false}
    console.log(gen.next()); / {value: 5, done: false}
    console.log(gen.next()); / {value: 6, done: false}
    console.log(gen.next()); / {value: undefined, done: true}
    

    The value of yield* expression itself

    yield* is an expression, not a statement, so it evaluates to a value.

    js
    function* g4() {
      yield* [1, 2, 3];
      return "foo";
    }
    
    function* g5() {
      const g4ReturnValue = yield* g4();
      console.log(g4ReturnValue); / 'foo'
      return g4ReturnValue;
    }
    
    const gen = g5();
    
    console.log(gen.next()); / {value: 1, done: false}
    console.log(gen.next()); / {value: 2, done: false}
    console.log(gen.next()); / {value: 3, done: false} done is false because g5 generator isn't finished, only g4
    console.log(gen.next()); / {value: 'foo', done: true}
    

    Use with async generators

    js
    async function* g1() {
      await Promise.resolve(0);
      yield "foo";
    }
    
    function* g2() {
      yield "bar";
    }
    
    async function* g3() {
      / Can use yield* on both async and sync iterators
      yield* g1();
      yield* g2();
    }
    
    const gen = g3();
    
    console.log(await gen.next()); / {value: "foo", done: false}
    console.log(await gen.next()); / {value: "bar", done: false}
    console.log(await gen.next()); / {done: true}
    

    Method forwarding

    The next(), throw(), and return() methods of the current generator are all forwarded to the underlying iterator.

    js
    const iterable = {
      [Symbol.iterator]() {
        let count = 0;
        return {
          next(v) {
            console.log("next called with", v);
            count++;
            return { value: count, done: false };
          },
          return(v) {
            console.log("return called with", v);
            return { value: "iterable return value", done: true };
          },
          throw(v) {
            console.log("throw called with", v);
            return { value: "iterable thrown value", done: true };
          },
        };
      },
    };
    
    function* gf() {
      yield* iterable;
      return "gf return value";
    }
    
    const gen = gf();
    console.log(gen.next(10));
    / next called with undefined; the argument of the first next() call is always ignored
    / { value: 1, done: false }
    console.log(gen.next(20));
    / next called with 20
    / { value: 2, done: false }
    console.log(gen.return(30));
    / return called with 30
    / { value: 'iterable return value', done: true }
    console.log(gen.next(40));
    / { value: undefined, done: true }; gen is already closed
    
    const gen2 = gf();
    console.log(gen2.next(10));
    / next called with undefined
    / { value: 1, done: false }
    console.log(gen2.throw(50));
    / throw called with 50
    / { value: 'gf return value', done: true }
    console.log(gen.next(60));
    / { value: undefined, done: true }; gen is already closed
    

    If the return()/throw() method of the underlying iterator returns done: false, the current generator continues executing and yield* continues to delegate to the underlying iterator.

    js
    const iterable = {
      [Symbol.iterator]() {
        let count = 0;
        return {
          next(v) {
            console.log("next called with", v);
            count++;
            return { value: count, done: false };
          },
          return(v) {
            console.log("return called with", v);
            return { value: "iterable return value", done: false };
          },
        };
      },
    };
    
    function* gf() {
      yield* iterable;
      return "gf return value";
    }
    
    const gen = gf();
    console.log(gen.next(10));
    / next called with undefined
    / { value: 1, done: false }
    console.log(gen.return(20));
    / return called with 20
    / { value: 'iterable return value', done: false }
    console.log(gen.next(30));
    / { value: 2, done: false }; gen is not closed
    

    If the underlying iterator doesn't have a throw() method and the generator's throw() is called, yield* throws an error.

    js
    const iterable = {
      [Symbol.iterator]() {
        let count = 0;
        return {
          next(v) {
            count++;
            return { value: count, done: false };
          },
        };
      },
    };
    
    function* gf() {
      yield* iterable;
      return "gf return value";
    }
    
    const gen = gf();
    gen.next(); / First next() starts the yield* expression
    gen.throw(20); / TypeError: The iterator does not provide a 'throw' method.
    

    Specifications

    Specification
    ECMAScript® 2026 Language Specification
    # sec-generator-function-definitions-runtime-semantics-evaluation

    Browser compatibility

    See also