简体   繁体   English

Q.所有请求承诺数组,不确定如何获取结果

[英]Q.all array of request promises, not sure how to get the results

Probably an obvious answer to this but I'm not sure what way to take. 对此可能是一个明显的答案,但是我不确定该采取什么方法。 request is a node module: https://github.com/request/request I fill an array of getHistory requests (with different parameters). request是一个节点模块: https : //github.com/request/request我填充了一组getHistory请求(具有不同的参数)。 p = [p1,p2...]. p = [p1,p2 ...]。

this.app.all('/api/heatmap', function(req,res) {
   // fill p here _.each(blabla, p.push(gethistory(someparams...)
   var result = [];
   function getHistory(params) {
        var options = { ...};
        var callback = function(error, response, body) {
            if(error) { //wtv 
            } else {
              // what to do with the body here ? return body ? result.push(body) ?
            }
        }

        request(options, callback);
    }

    Q.all(p).then(function() {
    });
}

So the problem here is that I when all of the request to be done , put everything in an array/object then send the whole thing to the client. 所以这里的问题是,当所有请求都完成后,我将所有内容放入数组/对象中,然后将整个内容发送给客户端。 How to have getHistory returning the fetched value (after the request is done ). 如何使getHistory返回获取的值(完成请求后)。

Hope it's clear. 希望清楚。

The core problem here is that node.js-style callbacks and promises are not compatible. 这里的核心问题是node.js样式的回调和Promise不兼容。 Promises emphasize on return values, node emphasizes on callbacks. Promise强调返回值,node强调回调。

Therefore you need a sort of adapter that wraps node's callback convention properly, a process called Promisifcation . 因此,您需要一种适当包装节点的回调约定的适配器,称为Promisifcation的过程。 This can be done manually, but it's tedious at best and error-prone when you are not careful. 这可以手动完成,但是充其量是乏味的,如果您不小心的话,容易出错。 Luckily, since node's conventions are well-established, it can be automated. 幸运的是,由于节点的约定已经很完善,因此可以将其自动化。 Q has a few helpers for that, but Bluebird is quite a bit more convenient in this regard. Q为此提供了一些帮助 ,但是Bluebird在这方面更加方便。

So the easy way to do it is to switch to Bluebird as the promise library and to use promisifyAll . 因此,简单的方法是切换到作为承诺库的Bluebird并使用promisifyAll

var Promise = require('bluebird');
var request = Promise.promisifyAll(require("request"));

this.app.all('/api/heatmap', function(req, res) {
    var requests = blabla.map(function (item) {
        return request.getAsync({ /* params */ });
    });

    Promise.all(requests).then(function (responses) {
        res.send( JSON.stringify(responses) ); // whatever
    }).catch(function (error) {
        res.send( "An error ocurred: " + error ); // handle error
    });
}

FWIW, here's another answer that shows how the same would look like when done properly with Q: FWIW,这是另一个答案,显示了用Q正确完成后的效果:

// promisified request
function requestAsync(options) {
    var result = Q.defer();
    request(options, function(error, response, body) {
        if (error) {
            result.reject(error);
        } else {
            result.resolve(body);
        }
    });
    return result.promise;
}

// returns promises for heatmapVolumes
function getHistory(params) {
    return requestAsync({
        method: 'GET',
        url: 'https://api.kaiko.com/v1/charts/' + 
            encodeURIComponent(params.exchange) + '/' + 
            encodeURIComponent(params.pair) +'/history',
        qs: params.qs,
        headers: {
            "Content-Type": "application/json",
            "Accept": "application/json"
        }
    }).then(function (body) {
        return heatmapVolume(body, params.exchange, params.pair);
    }).catch(function (error) {
        // log detailed error and send less revealing message downstream
        console.error('error fetching trades', error);
        throw new Error('Something went wrong while fetching trades');
    });
}

// usage
this.app.all('/api/heatmap', function(req, res) {
    getHistory({
        exchange: "foo", pair: "bar", qs: "qux"
    }).then(function (heatmap) {
        res.send(200, heatmap);
    }).catch(function (error) {
        res.send(500, error);
    });
});

Used Q.deferred and it worked as documented \\o/ 使用了Q.deferred并按照\\ o /

function getHistory(params) {
                var deferred = Q.defer();
                var options = {
                    method: 'GET',
                    url: 'https://api.kaiko.com/v1/charts/' + params.exchange + '/' + params.pair +'/history',
                    qs:qs,
                    headers: {
                        "Content-Type": "application/json",
                        "Accept": "application/json"
                    }
                }

                var callback = function(error, response, body) {
                    if(error) {
                        console.log('error fetching trades', error);
                        res.send(500, 'Something went wrong while fetching trades');
                    } else {
                        var body = heatmapVolume(body, params.exchange, params.pair);
                        // console.log("result!", body.exchange, body.pair);
                        result.push(body);
                        // return body;
                        deferred.resolve();
                    }
                }

                request(options, callback);

                return deferred.promise;
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM