0

I'm using super-agent in node to make calls to a public API, was wondering if there's a way to make synchronous calls with it? I like it as it has automatic caching in all the calls, but i've looked online and it doesn't seem to have synchronous calls.

I'm doing 2 chunks of logic, both with for loops containing multiple calls to the API. I want to wait for the first for loop with multiple calls to be finished, so all the information from it can be passed to the second for loop.

My code is a little complicated and not very understandable sorry, but here it is anyway.

// Declared all of the variables up here
var request = require('superagent-cache')();

for (i = 0; i < summonerNameArray.length; i++) {
    console.log("Count: " + i);
    summonersToSendToBasic.push(summonerNameArray[i].summonerName);

    // Every 10 we can send the basic request to RIOT's API 
    if (i % 9 == 0 && i != 0 || i == summonerNameArray.length - 1) {
        console.log("Sending these summoners to basic API call: " + summonersToSendToBasic);
        var URL = "https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/" 
            + summonersToSendToBasic 
            + "?api_key=" 
            + APIKey;

        request
        .get(URL)
        .end(function(err, res) {
            if (res.statusCode != 404 || res.statusCode != 400) {
                console.log(res.body);
                console.log(JSON.stringify(res.body));

                //TODO ~~ Fix this to make it more efficient
                for (var username in res.body) {
                    console.log(res.body[username].id);
                    IDsToSendToRanked.push(res.body[username].id);
                }
            } 
        });

        // Clear the Array to start the whole process over again
        summonersToSendToBasic = [];
    }
}


for (i = 0; i < IDsToSendToRanked.length; i++) {
    //TODO - Work out how to nest these, should be fine but what is actually happening? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    IDsToSendToRankedCounted.push(IDsToSendToRanked[i]);

     if (i % 39 == 0 && i != 0 || i == IDsToSendToRanked.length - 1) {
        // Here we add them to IDsToSendToRankedCounted
        console.log("Sending these summoners to ranked API call: " + IDsToSendToRankedCounted);
        var URL = "https://euw.api.pvp.net/api/lol/euw/v2.5/league/by-summoner/" 
            + summonersToSendToBasic 
            + "/entry?api_key=" 
            + APIKey;

        request
        .get(URL)
        .end(function(err, res) {
            if (res.statusCode != 404 || res.statusCode != 400) {
                console.log(res.body);

                finalUniversityArray.push(res.body);
            } 
        });

        IDsToSendToRankedCounted = [];
     }
}

Basically I want IDsToSendToRanked to contain all of the IDs, and only then go onto the second for loop.

Olly
  • 360
  • 3
  • 20
  • I would wrap each request in a functor, wrap the functor in a `Promise` and then use the reduce technique from [this answer](http://stackoverflow.com/a/20105079/691711) to have each Promise resolve after each other. – zero298 Mar 09 '16 at 23:35
  • I agree that using promises here makes the most sense. You can queue up a bunch of promise-wrapped requests in an array and then `promise.all()` on the array to block the second for loop until the requests in the first for loop complete. – jpodwys May 05 '16 at 17:38

1 Answers1

0

As far as I know it isn't possible to have synchronous requests. Anyway there are other means to get to the result you want.

If you can introduce more libraries into your project, then I would advise you to use for example this library: https://www.npmjs.com/package/promise for promises this way you can do something similar to this:

var promiseOfIds = new Promise(function(resolve, reject) {
    request.get(url).end(function(err, response){
        if(err) {
            reject(err);
        } else {
            // Here you can modify the response as you like. 
            resolve(response)
        }
    })
});
promiseOfIds.then(function(ids){
    // DO the second request with loaded ids. 
});
Jakub Balhar
  • 315
  • 1
  • 7