check if function is a generator

后端 未结 12 1395
-上瘾入骨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:12

    In the latest version of nodejs (I verified with v0.11.12) you can check if the constructor name is equal to GeneratorFunction. I don't know what version this came out in but it works.

    function isGenerator(fn) {
        return fn.constructor.name === 'GeneratorFunction';
    }
    

提交回复
热议问题