generator-function

Expose the GeneratorFunction constructor if supported by the runtime

Usage no npm install needed!

<script type="module">
  import generatorFunction from 'https://cdn.skypack.dev/generator-function';
</script>

README

generator-function

Expose the ES2015 GeneratorFunction constructor.

Build Status Dependency Status devDependency Status npm version

Installation

npm install generator-function

Description

Like Functions, one could create generator functions from a JavaScript string through a special constructor called GeneratorFunction. However, the constructor function is not available as a global, so one has to use the constructor property of an existing generator function.

This module makes it easy to do so, and falls back gracefully when a runtime not supporting generator functions is encountered.

Usage

On a platform supporting generator functions:

var GeneratorFunction = require('generator-function')

var generatorFunction = new GeneratorFunction('arg1', 'yield arg1')
var generator = generatorFunction(1)

generator.next()
//=> { value: 1, done: false }
generator.next()
//=> { value: undefined, done: true }

On a runtime that does not support them:

var GeneratorFunction = require('generator-function')

GeneratorFunction
// => undefined

License

MIT