问题与蓝鸟和请求得到rest回应

我使用'请求'模块来请求以下代码的rest服务:

var request = require('request'); request.get('http://localhost:8190/api/1.0/product/012345', { auth: { user: 'toto', pass: 'totopass'} }, function(error,response,body) { console.log(body); }); 

它的工作:)但我必须确保该调用是同步的,所以我想使用一个承诺。 我写了下面的代码:

 var Promise = require('bluebird'); var request = Promise.promisifyAll(require('request')); request.getAsync('http://localhost:8190/api/1.0/product/012345', { auth: { user: 'toto', pass: 'totopass'} }).then(function(error,response,body) { console.log(body); }); 

但它失败了,我看到“未定义”是控制台。

它看起来像promisifyAll默认情况下删除err参数,并只返回一个参数。 multiArgs时尝试设置multiArgs ,然后使用spread将结果数组传递到下一个函数,并将error handling移动到catch ,如下所示:

 var Promise = require('bluebird'); var request = Promise.promisifyAll(require('request'), {multiArgs: true}); request.getAsync('http://localhost:8190/api/1.0/product/012345', { auth: { user: 'toto', pass: 'totopass'} }) .spread(function(response, body) { console.log(body); }) .catch(function(err){ console.log(err); });