简体   繁体   English

在父http请求中完成所有子http请求后,如何返回

[英]How can I return once all child http requests complete in a parent http request

I'm hitting an API which returns all details on kills in a game, the first endpoint returns an id to the kill event, then a second endpoint is hit to retrieve the killer and killed names. 我正在点击一个API,它返回游戏中杀戮的所有细节,第一个端点返回一个id到kill事件,然后第二个端点被命中以检索杀手并杀死名字。

Because of the way this API is set up I need to make a request to first get the event ID and then wait for all id's in the returned array to get a result and then process the entire kill array: 由于这个API的设置方式,我需要首先获取事件ID,然后等待返回数组中的所有id获取结果,然后处理整个kill数组:

    requestify.get(url).then(function (response) {
        var events = [];
        if (response.body && response.body.length > 0) {
            data = JSON.parse(response.body);
            if (data.hasOwnProperty('events')) {
                events = data.events.map(function(event) {
                    return this.getDataForHeroKillId(event.id, function(killInfo) {
                        return { killer: killInfo.killer, killed: killInfo.killed, timestamp: event.time };
                    });
                }.bind(this));
                console.log('events is: ', events);
            }
        }
        return Promise.all(events);
    }.bind(this));

My getKillInformation function looks like this: 我的getKillInformation函数如下所示:

KillFeed.prototype.getKillInformation = function(id, cb) {
var data = null;
    requestify.get(url).then(function (response) {
        var event = {};
        if (response.body && response.body.length > 0) {
            data = JSON.parse(response.body);
            if (data.hasOwnProperty('Killer')) {
                event = { killer: data.Killer, killed: data.Killed};
            }
        }
        cb(event);
    });
};

In the second method I was hoping that I could callback the result of each child request and then once they had all been executed my new array would hold the data. 在第二种方法中,我希望我可以回调每个子请求的结果,然后一旦它们全部被执行,我的新数组将保存数据。 But due to the event driven nature of JS I found that my code block continues to return an empty events array as this code is obviously non blocking (understandably as blocking the event queue whilst making a HTTP request is not ideal). 但是由于JS的事件驱动特性,我发现我的代码块继续返回一个空事件数组,因为这个代码显然是非阻塞的(可以理解为阻塞事件队列,同时发出HTTP请求并不理想)。 How can I implement this? 我该如何实现呢?

One uses promises for this. 一个人使用承诺

requestify.get(url).then(function (response) {
    var events = [];

    if (response.body && response.body.length > 0) {
        var data = JSON.parse(response.body);
        if (data.hasOwnProperty('events')) {
            // Trying to process the kill information here
            events = data.events.map(function(event) {
                return this.getKillInformation(event.id).then(function(killInfo) {
                    return { killer: killInfo.killer, killed: killInfo.killed, timestamp: event['time1'] };
                });
            }.bind(this));
        }
    }

    return Promise.all(events);
});

KillFeed.prototype.getKillInformation = function(id) {
    var url = 'internal_url';
    return requestify.get(url).then(function (response) {
        if (response.body && response.body.length > 0) {
            var data = JSON.parse(response.body);
            if (data.hasOwnProperty('killer')) {
                return { killer: data.Killer, killed: data.Killed };
            }
        }
    });
};

You could use async and its waterfall method. 您可以使用async及其瀑布方法。 Async is a NodeJS module, but it can be used in browser, too. Async是一个NodeJS模块,但它也可以在浏览器中使用。

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

相关问题 如何使用 Axios 向 URL 发送多个 HTTP 请求,并在收到其中一个请求的响应后停止发送? - How can I send multiple HTTP requests to a URL using Axios and stop sending once I receive a response from one of the request? $ http请求父级到子级控制器 - $http request parent to child controllers 等待所有$ http请求在Angular JS中完成 - Wait for all $http requests to complete in Angular JS 如何防止多个HTTP请求一次触发 - How to prevent multiple http requests from firing all at once http请求完成后,如何从angularJS服务返回对象? - How to return object from angularJS service AFTER http request is complete? 如何等待 Http 请求完成? - how to wait for Http request complete? 如何使用 request npm 模块判断 http 请求是否完成? - How can I tell if http request is complete using the request npm module? 解析 CSV 的正确方法是什么,发出异步 http 请求,一旦所有请求返回,然后使用聚合数据? - What is the right way to parse a CSV, making asynchronous http requests, once all the requests return, to then use the aggregated data? 我们可以一次将表单数据全部传递到角度http请求吗 - Can we pass form data to angular http request all at once 如何减少HTTP请求? - How can I Make fewer HTTP requests?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM