check if function is a generator

后端 未结 12 1393
-上瘾入骨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条回答
  •  -上瘾入骨i
    2020-11-29 06:14

    function isGenerator(target) {
      return target[Symbol.toStringTag] === 'GeneratorFunction';
    }
    

    or

    function isGenerator(target) {
      return Object.prototype.toString.call(target) === '[object GeneratorFunction]';
    }
    

提交回复
热议问题