meteor - Synchronizing multiple async queries before returning?

后端 未结 1 791
心在旅途
心在旅途 2020-12-16 02:53

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,

相关标签:
1条回答
  • 2020-12-16 03:43

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

    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.

    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;
        }
    });
    

    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 ]
    

    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

    0 讨论(0)
提交回复
热议问题