Files
30-seconds-of-code/snippets/isGeneratorFunction.md
2022-05-26 09:55:17 +03:00

24 lines
620 B
Markdown

---
title: Value is generator function
tags: type,function
expertise: intermediate
author: chalarangelo
cover: blog_images/interior-4.jpg
firstSeen: 2020-08-07T15:40:38+03:00
lastUpdated: 2020-10-20T11:21:07+03:00
---
Checks if the given argument is a generator function.
- Use `Object.prototype.toString()` and `Function.prototype.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
```