JavaScript Generator Methods

Beginner
4 min read
Updated: Nov 2026
3 method guides
Method index

About This Index

This page is the JavaScript Generator Methods index. A generator object has three methods. Each row below links to a full guide with syntax, examples you can run, and FAQs.

Learn what a generator is on /js/generator. Use this page when you need a specific method such as next().

All three are called on a generator object.

First call a function* function to get the object: const gen = myGenerator();. Then use gen.next(), gen.return() or gen.throw().

How to Call a Generator Method

Create a generator object, then call the methods on it:

JavaScript
function* demo() {
  try {
    yield 1;
    yield 2;
    yield 3;
  } finally {
    console.log("cleanup");
  }
}

const first = demo();
console.log(first.next().value);          // 1   (resume to the next yield)
console.log(first.return("stop").value);  // stop (finish early, finally runs)

const second = demo();
second.next();

try {
  second.throw(new Error("Oops"));        // throw an error at the paused yield
} catch (err) {
  console.log("Caught: " + err.message);
}

JavaScript Generator Methods Table

All method guides in this JavaScript tutorial series (3 tutorials), grouped by what they do.

Resume the generator

1 method

Run the generator until the next yield.

MethodDescriptionTutorial
next()Run to the next yield and return { value, done }. You can send a value into the generator.Open

Stop or interrupt the generator

2 methods

End the generator early, or send an error into it.

MethodDescriptionTutorial
return()Finish the generator early with a final value. Its finally blocks still run.Open
throw()Throw an error at the paused yield, where the generator can catch it.Open

Frequently Asked Questions

Three: next() resumes the generator and returns { value, done }, return() finishes it early, and throw() throws an error at the paused yield. A generator object is also iterable, so it works with for...of and the spread operator.
The Generator page explains the idea: function*, yield, laziness and the loops that use generators. This index lists the method guides so you can jump straight to next(), return() or throw().
No. They are instance methods: you call them on a generator object, as in gen.next(), after creating it with gen = myGenerator(). You do not call them on the generator function itself.
Not always. for...of, the spread operator and Array.from() call next() for you. Call it by hand when you want to send values in, pull only a few values, or look at the done flag.
Start with next(), because every generator uses it. Then read return() to learn about early exit and cleanup, and finish with throw() for error handling.

Start with next()

Open the guide for running a generator step by step: syntax, examples, and FAQs.

next() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful