https://github.com/timothygu/generator-function
Expose the GeneratorFunction constructor if supported by the runtime
https://github.com/timothygu/generator-function
Last synced: 10 months ago
JSON representation
Expose the GeneratorFunction constructor if supported by the runtime
- Host: GitHub
- URL: https://github.com/timothygu/generator-function
- Owner: TimothyGu
- License: mit
- Created: 2015-10-11T05:35:51.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-10-12T06:58:00.000Z (over 10 years ago)
- Last Synced: 2025-07-14T05:03:36.775Z (11 months ago)
- Language: JavaScript
- Size: 117 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# generator-function
Expose the ES2015 `GeneratorFunction` constructor.
[](https://travis-ci.org/TimothyGu/generator-function)
[](https://david-dm.org/TimothyGu/generator-function)
[](https://david-dm.org/TimothyGu/generator-function#info=devDependencies)
[](https://www.npmjs.org/package/generator-function)
## Installation
npm install generator-function
## Description
Like `Function`s, 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][mdn], 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.
[mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/GeneratorFunction#Summary
## Usage
On a platform supporting generator functions:
```js
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:
```js
var GeneratorFunction = require('generator-function')
GeneratorFunction
// => undefined
```
## License
MIT