Generators

suggest change

Introduction

Generator functions, declared with function*, can pause at yield expressions and resume later. Calling one returns a generator object that implements both the iterator and iterable protocols.

Syntax

function* name(parameters) {
  yield value;
  return finalValue;
}

const generator = name(arguments);
generator.next(value);
generator.return(value);
generator.throw(error);

Remarks

The object returned by next(), return(), or throw() has value and done properties. A generator runs synchronously between suspension points; use an async generator (async function*) when producing values requires asynchronous work.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents