How to write a module for compatibility with Bluebird `PromisifyAll`

Say, in the node.js module,, moduleA.jsI have the following object with a bunch of node-line asynchronous functions:

// moduleA.js

var init = function (data, callback) {
    return callback(null, data.params );
};

var delay = function(data, callback) {
    setTimeout(function(){
        callback(null, data);
    }, 1000*3);
}


var reverse = function(data, callback) {
    var j,
        d = {};

    for(j in data) {
        d[ data[j] ] = j;
    }

    callback(null, d);

}

module.exports = {
    init: init,
    delay: delay,
    reverse: reverse
};

I consume moduleA.jsin main.jsand can successfully promisifyeach method individually, for example:

// main.js
var Promise = require('bluebird'),
    modA = require('./moduleA') );

var data = {
    "a": "one",
    "b": "two",
    "c": "three",
    "d": "four"
};


Promise.promisify(modA.init)(data)

    .then( Promise.promisify(modA.delay) )

    .then( Promise.promisify(modA.reverse) )

    .then(function(data){

        res.send(data);

    }).catch(function(e){
        next(e);
    });

The above code works just fine, but more verbose than we would like.

My question is: how can I change my module to allow it Promise.promisifyAllto work correctly on the whole exported object? I want to avoid promisificationinside the module and allow others if necessary promisifyto consume it.

I tried unsuccessfully many options:

// main.js
var Promise = require('bluebird'),
    modA = require('./moduleA') ),
    modAPromised = Promise.promisifyAll(modA);

var data = {
    "a": "one",
    "b": "two",
    "c": "three",
    "d": "four"
};

modAPromised.initAsync(data)

    .then(modAPromised.delayAsync)

    .then(modAPromised.reverseAsync)

    .then(function(data){

        res.send(data);

    }).catch(function(e){
        next(e);
    });

When I do this, I get an error message Cannot call method 'delay' of undefined. Promise.promisifyAllAdds all functions Asyncas expected:

// console.log(modAPromised);

{
    init: [Function],
    delay: [Function],
    reverse: [Function],
    initAsync: {
        [Function] __isPromisified__: true
    },
    delayAsync: {
        [Function] __isPromisified__: true
    },
    reverseAsync: {
        [Function] __isPromisified__: true
    }
}

, - . -, delayAsync this.delay, this undefined.

, , Promise.promisifyAll ?

.

+4
2

promisifyAll , this, this , , .

:

Promise.promisifyAll(require("redis"));

// In another file
var redis = require("redis");
var client1 = redis.createClient(...);
var client2 = redis.createClient(...);

client1.putAsync(...);
client2.putAsync(...);

, "redis", client. .putAsync put - put client1, client2 , putAsync.

-, , : https://github.com/petkaantonov/bluebird/issues/470.


API, nodeify, , :

var init = function (data, callback) {
    return Promise.resolve(data.params).nodeify(callback);
};

var delay = function(data, callback) {
    return Promise.delay(data, 1000 * 3).nodeify(callback);
}

var reverse = function(data, callback) {
    var j,
        d = {};

    for(j in data) {
        d[ data[j] ] = j;
    }

    return Promise.resolve(d).nodeify(callback);
}
+1

moduleA.js main.js.

.then(), :

// main.js
var Promise = require('bluebird'),
    modA = require('./moduleA'),
    modAPromised = Promise.promisifyAll(modA);

var data = {
    "a": "one",
    "b": "two",
    "c": "three",
    "d": "four"
};

modAPromised.initAsync(data)

    .then(function(data) {
        return modAPromised.delayAsync(data);
    })

    .then(function(data) {
        return modAPromised.reverseAsync(data);
    })

    .then(function(data){

        res.send(data);

    }).catch(function(e){
        next(e);
    });

, . , promisifyAll , promise. , .

, Bluebird , .

0

All Articles