0

Can someone please explain to me why i'm getting this warning Warning: a promise was created in a handler but was not returned from it when I execute the following code:

cache['deviceSlave'].getBySystemId(systemId).then(function(slavesMapping) {

    // do other stuff

}).catch(function(err) {

    // throw error

});

Here is the rest of the code:

var Promise = require('bluebird');
var _ = require('lodash');
var Redis = require('ioredis');
var config = require('/libs/config');

var redis = new Redis({
    port: config.get('redis:port'),
    host: config.get('redis:host'),
    password: config.get('redis:key'),
    db: 0
});


var self = this;

module.exports.getBySystemId = function(systemId) {

    return new Promise(function(resolve, reject) {

        var systemIds = [systemId];

        self.getBySystemIds(systemIds).then(function(result) {

            return resolve(_.values(result)[0]);

        }).catch(function(err) {

            return reject(err);

        });

    });

};


module.exports.getBySystemIds = function(systemIds) {

    return new Promise(function(resolve, reject) {

        var pipeline = redis.pipeline();

        _.each(systemIds, function(systemId) {

            var cacheKey = 'device_slaves:' + systemId.replace(/:/g, '');

            // get through pipeline for fast retrieval
            pipeline.get(cacheKey);

        });


        pipeline.exec(function(err, results) {

            if (err) return reject(err);
            else {

                var mapping = {};

                _.each(systemIds, function(systemId, index) {

                    var key = systemId;
                    var slaves = JSON.parse(results[index][1]);

                    mapping[key] = slaves;


                });

                return resolve(mapping);

            }

        });


    });

};

I'm using the following libraries: ioredis & bluebird. The code executes fine and everything just works good! I just dont like the fact I get an warning which I can not solve!

MakanMakan
  • 81
  • 1
  • 2

1 Answers1

0

Bluebird is warning you against explicit construction here. Here is how you should write the above code:

module.exports.getBySystemId = function(systemId) {
  return self.getBySystemIds([systemId]).then(result => _.values(result)[0]);
};

There is no need to wrap the promise - as promises chain :)

Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • How stupid as it sounds, I really thought that this would solve my problem ... but it doesn't. :(, any other suggestions ? – MakanMakan May 15 '17 at 07:27