javascript - Express | Making multiple http requests asynchronously - Stack Overflow

I am currently building a small node application that makes a few api calls and renders a webpage with

I am currently building a small node application that makes a few api calls and renders a webpage with charts on it. I'm using express and jade as the render engine.

The problem is that I'm quite new to javascript and I don't know how to scheme out my http requests so I can pass an object of variables I got from the api (http get) when there is more than one request. I don't know how to map it out to make a single object and send it to the jade rendering engine.

Here is what I have so far :

app.get('/test', function(req, res) {
    apiRequestGoesHere(name, function(error, profile) {
        //Get some data here
    });
    anotherApiRequest(tvshow, function(error, list) {
        //Get some data here
    });
    res.render('test', data);
});

As it is right now, the page renders and the requests are not done yet, and if I place res.render inside one of the request, I can't access the other's data.

So what I want is a way to set it up so I can have multiple api calls, then make an object out of some elements of what is returned to me from the rest api and send it to Jade so I can use the data on the page.

I am currently building a small node application that makes a few api calls and renders a webpage with charts on it. I'm using express and jade as the render engine.

The problem is that I'm quite new to javascript and I don't know how to scheme out my http requests so I can pass an object of variables I got from the api (http get) when there is more than one request. I don't know how to map it out to make a single object and send it to the jade rendering engine.

Here is what I have so far :

app.get('/test', function(req, res) {
    apiRequestGoesHere(name, function(error, profile) {
        //Get some data here
    });
    anotherApiRequest(tvshow, function(error, list) {
        //Get some data here
    });
    res.render('test', data);
});

As it is right now, the page renders and the requests are not done yet, and if I place res.render inside one of the request, I can't access the other's data.

So what I want is a way to set it up so I can have multiple api calls, then make an object out of some elements of what is returned to me from the rest api and send it to Jade so I can use the data on the page.

Share Improve this question asked Apr 15, 2014 at 4:50 user2663041user2663041 2295 silver badges13 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

You probably want to use async to help with this. async.parallel is a good choice for something simple like this:

app.get('/test', function(req, res) {
    async.parallel([
    function(next) {
        apiRequestGoesHere(name, function(error, profile) {
            //Get some data here
            next(null, firstData);
        });
    },
    function(next) {
        anotherApiRequest(tvshow, function(error, list) {
            //Get some data here
            next(null, secondData);
        });
    }], function(err, results) {
        // results is [firstData, secondData]
        res.render('test', ...);
    });
});

The first argument to those functions next should be an error if relevant (I put null) - as soon as one is called with an error, the final function will be called with that same error and the rest of the callbacks will be ignored.

You can async parallel.

async.parallel([
    function(callback){
        // Make http requests
        // Invoke callback(err, result) after http request success or failure
    },
    function(callback){
        // Make http requests
        // Invoke callback(err, result) after http request success or failure
    }
],
// optional callback
function(err, results){
    // the results array will be array of result from the callback
});

The reason your page renders is the callbacks haven't "called back" yet. To do what you want, you would need to do something like:

app.get('/test', function(req, res) {
    apiRequestGoesHere(name, function(error, profile) {
        //Get some data here
        anotherApiRequest(tvshow, function(error, list) {
            //Get some data here
            res.render('test', data);
        });
    });
});

This strategy leads to what is known as "pyramid code" because your nested callback functions end up deeper and deeper.

I would also remend the step library by Tim Caswell. It would make your code look something like:

var step = require('step');

app.get('/test', function(req, res) {
    step(
        function () {
            apiRequestGoesHere(name, this)
        },
        function (error, profile) {
            if error throw error;
            anotherApiRequest(tvshow, this)
        },
        function done(error, list) {
            if error throw error;
            res.render('test', list)
        }
    )
});

You could also use the group method to make the calls in parallel and still maintain the sequence of your callbacks.

Gl, Aaron

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742341841a4425784.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信