generator-bind

1.0.1 • Public • Published

generator-bind

Build Status

A bind polyfill that returns a GeneratorFunction when the function being bound is a generator.

Installation

npm install generator-bind

Rant

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.

Usage

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.

License

Copyright (c) 2014 Michael Diolosa <michael.diolosa@gmail.com>

This library is licensed under the MIT license.

Dependencies (0)

    Dev Dependencies (0)

      Package Sidebar

      Install

      npm i generator-bind

      Weekly Downloads

      13

      Version

      1.0.1

      License

      MIT

      Last publish

      Collaborators

      • mbrio