Detect Generator Functions with JavaScript

In the current JavaScript climate of new syntaxes, features, and using loads of external libraries, it seems harder than ever to be sure what your functions are being given or even what the data represents. Of course, we’ve come up with Flow and TypeScript to help, but we can’t count on those always being available. […]

The post Detect Generator Functions with JavaScript appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh

In the current JavaScript climate of new syntaxes, features, and using loads of external libraries, it seems harder than ever to be sure what your functions are being given or even what the data represents. Of course, we’ve come up with Flow and TypeScript to help, but we can’t count on those always being available. That’s why I like doing my own detection with JavaScript, especially when it comes to function types.

To detect if a function is a generator or async generator function, you can use the following code:

// Sample generator function
function* sampleGenerator() {}
sampleGenerator.constructor.name
// "GeneratorFunction"

async function* sampleGenerator() {}
sampleGenerator.constructor.name
// "AsyncGeneratorFunction"

Coincidentally, you can also detect a regular async function with:

async function asyncThing() {}
asyncThing.constructor.name
// "AsyncFunction"

It’s always important to know if the code you’re using is sync, async, or a generator, but if you’re using external libraries or want to write comprehensive tests, these types of detections may be necessary.

The post Detect Generator Functions with JavaScript appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh


Print Share Comment Cite Upload Translate Updates
APA

David Walsh | Sciencx (2020-11-23T12:51:24+00:00) Detect Generator Functions with JavaScript. Retrieved from https://www.scien.cx/2020/11/23/detect-generator-functions-with-javascript/

MLA
" » Detect Generator Functions with JavaScript." David Walsh | Sciencx - Monday November 23, 2020, https://www.scien.cx/2020/11/23/detect-generator-functions-with-javascript/
HARVARD
David Walsh | Sciencx Monday November 23, 2020 » Detect Generator Functions with JavaScript., viewed ,<https://www.scien.cx/2020/11/23/detect-generator-functions-with-javascript/>
VANCOUVER
David Walsh | Sciencx - » Detect Generator Functions with JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2020/11/23/detect-generator-functions-with-javascript/
CHICAGO
" » Detect Generator Functions with JavaScript." David Walsh | Sciencx - Accessed . https://www.scien.cx/2020/11/23/detect-generator-functions-with-javascript/
IEEE
" » Detect Generator Functions with JavaScript." David Walsh | Sciencx [Online]. Available: https://www.scien.cx/2020/11/23/detect-generator-functions-with-javascript/. [Accessed: ]
rf:citation
» Detect Generator Functions with JavaScript | David Walsh | Sciencx | https://www.scien.cx/2020/11/23/detect-generator-functions-with-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.