Skip to content

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

License

Notifications You must be signed in to change notification settings

mbrio/generator-bind

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 <[email protected]>

This library is licensed under the MIT license.

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published