Add isGeneratorFunction
This commit is contained in:
18
snippets/isGeneratorFunction.md
Normal file
18
snippets/isGeneratorFunction.md
Normal file
@ -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
|
||||
```
|
||||
Reference in New Issue
Block a user