-1

Im building an app with Nodejs. Im fairly fluent with Front end javascript where asynchronous events rarely get too complex and don't go that deep. But now that I'm using Node which is all event driven, making a lot of calls to different servers and databases that all rely on each other becomes rather clustered.

It seems to be common place to have a next() function passed as a parameter that gets called once the first event has finished. This works great however I'm struggling to keep readable code when needing to have next functions after next functions.

Let me explain through example.

Lets say I have a route defined like so:

app.use('/fetchData', function(req, res) {

});

So before we can return the data I need to make a few async calls.

  • First to the database to retrieve login details.

  • Then using the login details i need to make another call to an external server to login in and retrieve the raw information.

  • Then third I need to go back to the database to do some checks.

    And then finally return the data to the user.

How would you do that? Im trying like this but cant get it right nor looking readable:

app.use('/fetchData', function(req, res) {
    
    //First I create a user object to pass information around to each function
    var user = {...};

    var third = database.doSomeChecks;

    var second = server.externalCall(user, third);

    //first
    database.getLoginDetails(user, second);
});

Obviously second actually runs the function and sets second as the returned value. But I can seem to pass the right information through to second.

One Option i thought could be to pass through an array of callbacks and to always call the last function in the array and remove it.

app.use('/fetchData', function(req, res) {
    
    //First I create a user object to pass information around to each function including the req and res object to finally return information
    var user = {...};

    var third = database.doSomeChecks;

    var second = server.externalCall;

    //first
    database.getLoginDetails(user, [third, second]);
});

What are your techniques? Is the array idea as pointed out above the best solution?

Community
  • 1
  • 1
Dustin Silk
  • 4,320
  • 5
  • 32
  • 48

1 Answers1

0

I'd recommend you to use promises as a personal preference I like to use bluebird it's easy to implement, it has a very nice performance and also it has some cool features to play with.

with promises, it's easier to read the control flow execution (at least to me), a lot of people complain about the callback hell and promises it's one of the possible solutions.

you can do something like this:

from:

var user = {...};

var third = database.doSomeChecks;

var second = server.externalCall(user, third);

to:

var user = {...};
checkDB(query).then(
function(data){
    //data checks
    return value 
}).then(
    function(value){
        // value returned from the previous promise
        return server.externalCall(value);
    });

you can take a look to this answer and see how you can deal with nested promises which are far easier than callbacks.

I hope that helps.

Community
  • 1
  • 1
pedrommuller
  • 15,741
  • 10
  • 76
  • 126