check if function is a generator

后端 未结 12 1400
-上瘾入骨i
-上瘾入骨i 2020-11-29 06:03

I played with generators in Nodejs v0.11.2 and I\'m wondering how I can check that argument to my function is generator function.

I found this way typeof f ===

12条回答
  •  抹茶落季
    2020-11-29 06:04

    I checked how koa does it and they use this library: https://github.com/ljharb/is-generator-function.

    You can use it like this

    const isGeneratorFunction = require('is-generator-function');
    if(isGeneratorFunction(f)) {
        ...
    }
    

提交回复
热议问题