A bind
polyfill that returns a GeneratorFunction
when the function being bound is a generator.
npm install generator-bind
I found what I believe to be a deficiency in the bind
function in node v0.11 whereby calling bind
on a generator returns a normal function.
function normalFunc() {...}
normalFunc.constructor.name // Function
normalFunc.bind(null).constructor.name // Function
function* genFunc() {...}
genFunc.constructor.name // GeneratorFunction
genFunc.bind(null).constructor.name // Function
This can cause issues when attempting to use the various async libraries that utilize generators with an OOP design pattern.
function Klass() {...}
Klass.prototype.someAsync = function* () {...}
let inst = new Klass();
co(inst.someAsync.bind(inst))()
The above code will cause an error with co
because the native bind
function does not return a generator.
This is why I've built generator-bind.
It can be used as a function:
let genbind = require('generator-bind');
genbind(ctx, genFunc);
or a polyfill:
let genbind = require('generator-bind').polyfill();
genFunc.bind(ctx);
The polyfill also returns the new bind
function so if you choose to use the polyfill you have the option of using the function as well.
Copyright (c) 2014 Michael Diolosa <[email protected]>
This library is licensed under the MIT license.