javascriptpromisebrowserifywhen-js

Chain promises with then()


I'm using the when promise library to lift() my node style callbacks and promisify them...

var nodefn = require('when/node');

var one = nodefn.lift(oneFn),
    two = nodefn.lift(twoFn),
    three = nodefn.lift(threeFn);


function oneFn(callback){
    console.log('one');
    callback(null);
}

function twoFn(callback){
    console.log('two');
    callback(null);
}

function threeFn(callback){
    console.log('three');
    callback(null);
}

I want to call the functions in a chain, like this:

one().then(two).then(three).done();

But it gives me an error when calling the second callback function:

Uncaught TypeError: undefined is not a function

The error refers to the callback function within twoFn(callback).

What is the correct way to chain these functions together and execute one after the other?


Solution

  • The problem is that nodefn.lift doesn't know how many parameters the function has (zero), so it just takes the appearing arguments and appends its callback to them. In a then chain every callback receives the result of the previous promise (in your case: undefined), so your twofn will be called with two arguments: undefined and the nodeback.

    So if you fix their arities, it should work:

    Function.prototype.ofArity = function(n) {
        var fn = this, slice = Array.prototype.slice;
        return function() {
            return fn.apply(null, slice.call(arguments, 0, n));
        };
    };
    var one = nodefn.lift(oneFn).ofArity(0),
        two = nodefn.lift(twoFn).ofArity(0),
        three = nodefn.lift(threeFn).ofArity(0);