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.