Sign in
Log inSign up
In Node.js how to have a better control flow when dealing with async database calls?

In Node.js how to have a better control flow when dealing with async database calls?

Robert's photo
Robert
·Mar 23, 2016

Please take a look at this code :

function(req,res){
    Article.find(id,function(err,article){
        if(article.isPrivate){
            //Make an async call here and do something
            //I also want to execute #9 here
        }

        //Some task here... #9
    });
}

As you can see, control flow becomes weird when you have complex conditions and async calls.

What do you recommend? Is there any way I can make async calls look like synchronous ones and have a better control flow?

Thanks for your time.