From 338bce1ed70272cf1c1a90c8d06196d3c36bb1c4 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 7 Aug 2020 15:40:38 +0300 Subject: [PATCH] Add isGeneratorFunction --- snippets/isGeneratorFunction.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 snippets/isGeneratorFunction.md 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 +```