流星 - 返回前同步多个异步查询? [英] meteor - Synchronizing multiple async queries before returning?

查看:152
本文介绍了流星 - 返回前同步多个异步查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个应该告诉服务器多个API请求发送到第三方的API,然后将这些查询结果组合成一个阵列,这是返回给客户端流星方法。

So I have a Meteor method that is supposed to tell the server to send multiple API requests to 3rd party APIs, and then combine the results of these queries into one array, which is returned to the client.

不过,我似乎无法找到服务器等到所有的API查询已返回结果前完成的方式。

However, I can't seem to find a way for the server to wait until all the API queries have completed before returning the result.

在code,刚刚取此起彼伏的数据API调用的同步版本是这样的:

The synchronous version of the code, which just fetches the data API call after another, goes like this:

Meteor.methods({
    fetchData: function(APILinks) {
        var data = [];
        APILinks.forEach(function(APILink) {
            var items = HTTP.get(APILink).content.items;
            items.forEach(function (item) {
                data.push(item);
            });
        });
        return items;
    }
});

此同步code ++工程。不过,我一直没能找到一个很好的方法,使API请求异步。我能得到的溶液最接近是重新定义以返回只有一个API请求的结果的方法,再有通过每个API链路的客户端环并要求他们中的每一个的方法。但是,有没有办法来包装这些请求到返回一个美好的方法的只有当所有的API请求完成

This synchronous code works. However, I haven't been able to find a good way to make the API requests async. The closest I could get to a solution was to redefine the method to return the result of only one API request, and then have the client side loop through each of the API link and calling the method for each one of them. However, is there a way to wrap all these requests into one nice method that returns only when all the API requests are complete?

推荐答案

您必须使用 HTTP.get 的异步版本,并使用<$ C $收集结果C>未来秒。

You have to use the asynchronous version of HTTP.get and collect the results using Futures.

我用的setTimeout s到模拟HTTP请求,让你明白一个道理,我建议你从这个code启动并更换假由一个简单的例子的setTimeout 您的HTTP GET请求。

I made up a simple example using setTimeouts to simulate HTTP requests so that you understand the principle, I advise you start from this code and replace the dummy setTimeout with your HTTP get request.

这个例子是将多任务(N)作为参数的测试服务器方法,然后启动每个计算其指数在广场N个任务指数秒。

The example is a test server method which takes a number of tasks (n) as a parameter, it then launches n tasks that each compute the square of their index in index seconds.

// we use fibers which is a dependency of Meteor anyway
var Future = Npm.require("fibers/future");

Meteor.methods({
    test: function(n) {
        // build a range of tasks from 0 to n-1
        var range = _.range(n);
        // iterate sequentially over the range to launch tasks
        var futures = _.map(range, function(index) {
            var future = new Future();
            console.log("launching task", index);
            // simulate an asynchronous HTTP request using a setTimeout
            Meteor.setTimeout(function() {
                // sometime in the future, return the square of the task index
                future.return(index * index);
            }, index * 1000);
            // accumulate asynchronously parallel tasks
            return future;
        });
        // iterate sequentially over the tasks to resolve them
        var results = _.map(futures, function(future, index) {
            // waiting until the future has return
            var result = future.wait();
            console.log("result from task", index, "is", result);
            // accumulate results
            return result;
        });
        //
        console.log(results);
        return results;
    }
});

键入&GT; Meteor.call(测试,3,功能(错误结果){执行console.log(结果);}); 在浏览器的控制台。这将输出 [0,1,4] 在3秒后。

Type > Meteor.call("test",3,function(error,result){console.log(result);}); in your browser console. This will output [0,1,4] after 3 seconds.

在您的服务器控制台,这将输出:

In your server console, this will output :

// immediately :
launching task 0
launching task 1
launching task 2
// after 1 second :
result from task 0 is 0
// after 2 seconds :
result from task 1 is 1
// after 3 seconds :
result from task 2 is 4
[ 0, 1, 4 ]

HTTP.get 异步版本流星文档详细说明:

The HTTP.get asynchronous version is detailed in Meteor docs :

http://docs.meteor.com/#http_call

如果你想更好地了解整个未来的概念,指的是纤维文档:

If you want to understand better the whole Future concept, refer to the fibers docs :

https://github.com/laverdet/node-fibers

这篇关于流星 - 返回前同步多个异步查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆