Test if your value is a ES6:Generator or ES6:GeneratorFunction
npm install is-generator-es6 - It doesn't use fn.name
- It handles values from another frame (browsers)
- It only checks [ES6] generators & [ES6] generator functions
- The module doesn't throw a SyntaxError when it is loaded in older browsers/servers thanks to new Function()
The function tests if your {value} is an instance of function\* (){} constructor.
If the code is executed inside a navigator and instanceof failed, the function will check if the argument begins with function\* to deal with value from another frame.
``js`
var isGeneratorFunction = require('is-generator-es6').isGeneratorFunction;
isGeneratorFunction(function* (){}) //returns true$3
Use Object.prototype.toString to ensure that {value} is a ES6:Generator after checking the presence of nextand throw.
`js`
var isGenerator = require('is-generator-es6');
isGenerator((function* (){})()) //returns true
Checks if value.__proto__.__proto__ is equal to (function*(){}).prototype.__proto__ after checking the presence of next and throw.
If the code is executed inside a browser, it will call Object.prototype.toString if the first condition failed.
This solution has better performances than the previous one, however it doesn't offer the same accuracy and it doesn't deal with prototype chaining.
`js`
var isGenerator = require('is-generator-es6')._isGenerator;
isGenerator((function* (){})()) //returns true
To deal with prototype chaining, one solution would be to implement a loop that goes deeper at each iteration until it hits a __proto__ that match (function*(){}).prototype.__proto__`.