承诺 bluebird 有多个 类 响应

Promise bluebird with multiple classes response

我试图向 url 发出请求并在请求后解析 XML 数据并打印结果,但我一直得到 undefined 而不是结果,我做错了什么?

var parseUrl = 'url which returns XML as response';
    var request = Promise.promisify(require("request"));
    var xml2js = Promise.promisify(require('xml2js').parseString);
    Promise.promisifyAll([request, xml2js]);

    request({url: parseUrl, json: true}).then(function(data){
        if (data.body){
            return data.body;
        } else {
            return error.throw('Failed to parse body from response');
        }
    }).then(function(data){
        xml2js(data, function(err, result){
            if (!err){
                return result;
            } else {
                return error.throw('Failed to read converted body from response');
            }
        });
    }).then(function(data){
        console.warn(data);
    }).catch(function(e){
        console.log(e.message);
    });

1) 因为 xml2js 已经 promisifiсation。

2) 您不需要使用 Promise.promisifyAll

var parseUrl = 'url which returns XML as response';
var request = Promise.promisify(require("request"));
var xml2js = Promise.promisify(require('xml2js').parseString);

request({url: parseUrl, json: true})
  .then(function(data){
    if (data.body){
        return data.body;
    } else {
        return error.throw('Failed to parse body from response');
    }
}).then(xml2js)
  .then(function(data){
    console.warn(data);
}).catch(function(e){
    console.log(e.message);
});