In node 7, you can instanceof from constructors to detect generator functions and asynchronous functions:
const GeneratorFunction = function*(){}.constructor; const AsyncFunction = async function(){}.constructor; function norm(){} function*gen(){} async function as(){} norm instanceof Function; // true norm instanceof GeneratorFunction; // false norm instanceof AsyncFunction; // false gen instanceof Function; // true gen instanceof GeneratorFunction; // true gen instanceof AsyncFunction; // false as instanceof Function; // true as instanceof GeneratorFunction; // false as instanceof AsyncFunction; // true
This works for all circumstances in my tests. The above comment says that it does not work for function expressions of the generated generators, but I cannot reproduce:
const genExprName=function*name(){}; genExprName instanceof GeneratorFunction;
The only problem is that the .constructor property of instances can be changed. If someone really decided to cause problems, they can break it:
// Bad people doing bad things const genProto = function*(){}.constructor.prototype; Object.defineProperty(genProto,'constructor',{value:Boolean}); // .. sometime later, we have no access to GeneratorFunction const GeneratorFunction = function*(){}.constructor; GeneratorFunction; // [Function: Boolean] function*gen(){} gen instanceof GeneratorFunction; // false
Lycan Nov 20 '16 at 13:01 2016-11-20 13:01
source share