check if function is a generator

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

    A difficulty not addressed on here yet is that if you use the bind method on the generator function, it changes the name its prototype from 'GeneratorFunction' to 'Function'.

    There's no neutral Reflect.bind method, but you can get around this by resetting the prototype of the bound operation to that of the original operation.

    For example:

    const boundOperation = operation.bind(someContext, ...args)
    console.log(boundOperation.constructor.name)       // Function
    Reflect.setPrototypeOf(boundOperation, operation)
    console.log(boundOperation.constructor.name)       // GeneratorFunction
    

提交回复
热议问题