Asynchronous HTTP Get Requests

I’ve got a template which requires retrieving responses (in JSON) from 3 HTTP get requests, but it’s done synchronously so they are each retrieved a couple of seconds after each other. I’ve tried doing it asynchronously but I can’t seem to return the response?

Here’s the code for doing it synchronously:

Meteor.methods({
fetchData1: function(param) {
var url = “”;
// Synchronous GET Request
var result = HTTP.get(url, {timeout:30000});
if(result.statusCode==200) {
var jsonResponse = JSON.parse(result.content);
return jsonResponse;
} else {
console.log("Response issue: ", result.statusCode);
var errorJson = JSON.parse(result.content);
throw new Meteor.Error(result.statusCode, errorJson.error);
}
},
fetchData2: function(param) {
var url = “”;
// Synchronous GET Request
var result = HTTP.get(url, {timeout:30000});
if(result.statusCode==200) {
var jsonResponse = JSON.parse(result.content);
return jsonResponse;
} else {
console.log("Response issue: ", result.statusCode);
var errorJson = JSON.parse(result.content);
throw new Meteor.Error(result.statusCode, errorJson.error);
}
},
fetchData3: function(param) {
var url = “”;
// Synchronous GET Request
var result = HTTP.get(url, {timeout:30000});
if(result.statusCode==200) {
var jsonResponse = JSON.parse(result.content);
return jsonResponse;
} else {
console.log("Response issue: ", result.statusCode);
var errorJson = JSON.parse(result.content);
throw new Meteor.Error(result.statusCode, errorJson.error);
}
}
});


And here’s the code I’ve been trying to use to do it asynchronously, however I’m unable to return the response:

Meteor.methods({

fetchData1: function(param) {
var url = “”;
// Asynchronous GET Request
var result = HTTP.get(url, {timeout:30000}, function(error, result) {
if(result.statusCode==200) {
console.log(“here”);
var jsonResponse = JSON.parse(result.content);
return jsonResponse;
} else {
console.log("Response issue: ", result.statusCode);
var errorJson = JSON.parse(result.content);
throw new Meteor.Error(result.statusCode, errorJson.error);
}
});
},
fetchData2: function(param) {
var url = “”;
// Asynchronous GET Request
var result = HTTP.get(url, {timeout:30000}, function(error, result) {
if(result.statusCode==200) {
var jsonResponse = JSON.parse(result.content);
return jsonResponse;
} else {
console.log("Response issue: ", result.statusCode);
var errorJson = JSON.parse(result.content);
throw new Meteor.Error(result.statusCode, errorJson.error);
}
});
},
fetchData3: function(param) {
var url = “”;
// Asynchronous GET Request
var result = HTTP.get(url, {timeout:30000}, function(error, result) {
if(result.statusCode==200) {
var jsonResponse = JSON.parse(result.content);
return jsonResponse;
} else {
console.log("Response issue: ", result.statusCode);
var errorJson = JSON.parse(result.content);
throw new Meteor.Error(result.statusCode, errorJson.error);
}
});
}
});


Any ideas?

You will not be able to return the response to the client asynchronously – that’s the nature of an asynchronous request, and why a callback is needed. Assuming you’d like to stick with the async processing server side for performance reasons, your best bet is to write the results to a collection or some other reactive data source on the server inside the callback of your async method, then simply subscribe to the collection on the client. Otherwise, if performance is not an issue and you want to simply return the results, I’d stick with the synchronous method (where you’ve omitted the callback). Hope that helps!