Reputation: 1014
So I'm currently building a small personal app for learning, I have a scenario where I was to use the API's of other third parties, but several for 1 object. For instance, I have 1 dashboard to render and I want to make different calls to different places and product an object of data back that contains the three request responses.
For example at the moment I have something (similar to) the below, which makes a request and returns that data, along with the other data from the find method and current user session. Works great.
app.get('/profile', isLoggedIn, function(req, res) {
Object.find().exec(function (err, records) {
request('firstUrl', function(err, resp, body) {
res.render('profile.ejs', {
user : req.user,
records : records,
responseData : JSON.parse(body)
});
});
});
});
But now I want to make more than 1 request...so I'm not sure about how to go about this and also best practice?
The below feels like it won't work and messy:
app.get('/profile', isLoggedIn, function(req, res) {
Object.find().exec(function (err, records) {
request('firstUrl', function(err, resp, body1) {
request('secondUrl', function(err, resp, body2) {
request('thirdUrl', function(err, resp, body3) {
res.render('profile.ejs', {
user : req.user,
records : records,
responseData1 : JSON.parse(body1),
responseData2 : JSON.parse(body2),
responseData3 : JSON.parse(body3)
});
});
});
});
});
});
Can anyone shed a bit of light on this?
Upvotes: 0
Views: 466
Reputation: 1309
You might find async.js to be useful in keeping your code clean from the "callback hell." Specifically the parallel or series methods.
For example (not tested):
async = require('async');
app.get('/profile', isLoggedIn, function(req, res) {
async.parallel({
records: function(callback) {
Object.find().exec(function (err, records) {
callback(null, records);
});
},
responseData1: function(callback) {
request('firstUrl', function(err, resp, body) {
callback(null, JSON.parse(body));
});
},
responseData2: function(callback) {
request('firstUrl', function(err, resp, body) {
callback(null, JSON.parse(body));
});
},
responseData3: function(callback) {
request('firstUrl', function(err, resp, body) {
callback(null, JSON.parse(body));
});
},
},
function(err, results) {
res.render('profile.ejs', {
user : req.user,
records : results.records,
responseData1 : results.responseData1,
responseData2 : results.responseData2,
responseData3 : results.responseData3
});
});
});
The parallel method can also take an array of functions instead of an object, which may also be useful for you. Take look at the documentation.
If you plan on having a variable number of URLs you fetch with request, it may make sense to refactor the code to use async.js's queue or cargo methods instead of repeating the same code for each URL you want to load.
Upvotes: 1