diff --git a/snippets/isGeneratorFunction.md b/snippets/isGeneratorFunction.md new file mode 100644 index 000000000..5824808a1 --- /dev/null +++ b/snippets/isGeneratorFunction.md @@ -0,0 +1,18 @@ +--- +title: isGeneratorFunction +tags: type,function,intermediate +--- + +Checks if the given argument is a generator function. + +Use `Object.prototype.toString()` and `Function.call()` and check if the result is `'[object GeneratorFunction]'`. + +```js +const isGeneratorFunction = val => + Object.prototype.toString.call(val) === '[object GeneratorFunction]'; +``` + +```js +isGeneratorFunction(function() {}); // false +isGeneratorFunction(function*() {}); // true +```