2017-05-12 39 views
0

有人可以請我解釋爲什麼我得到這個警告警告:一個承諾是在處理程序中創建的,但沒有從它返回當我執行以下代碼:ioredis藍鳥一個承諾是在處理程序中創建的,但沒有從它返回

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

    // do other stuff 

}).catch(function(err) { 

    // throw error 

}); 

這裏是代碼的其餘部分:

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); 

      } 

     }); 


    }); 

}; 

我使用下列庫:ioredis &藍鳥。代碼執行得很好,一切都很好!我只是不喜歡這個事實,我得到了一個我無法解決的警告!

回答

0

藍鳥在這裏警告你反對explicit construction。這裏是你應該怎麼寫上面的代碼:

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

有沒有必要換的承諾 - 爲承諾鏈:)

+0

多麼愚蠢,因爲它的聲音,我真的認爲這將解決我的問題。 ..但它沒有。 :(,還有其他建議嗎? – MakanMakan

相關問題