0

I am new to node js and I got stuck at a very basic problem. I have couple of functions where I use the output of one function as a query param in the next param

function getCdTypeByValue (table_name,kpi_type){
//some query runs here
return id
}
insertInsight = function(req,res){
var id = getCdTypeByValue (table_name,kpi_type);
//use id in a query for insert

}

In my code the insert statement in insertInsight , needs the id returned from getCdTypeByValue. So , I need this to be a synchronous call , what are the best practises for nodejs to do this.

Ayan Biswas
  • 1,641
  • 9
  • 39
  • 66
  • 1
    Learn how to use Promises and `await`. – SLaks Nov 28 '18 at 16:35
  • As @PatrickHund suggests please have a look at that post to learn about `Promise` and `async/await` patterns. Then, when you are very confident look here https://stackoverflow.com/a/36585554/758836, where I suggest a more complex approach with `Promise.all` to resolve multiple asynchronous calls. – loretoparisi Nov 28 '18 at 16:41
  • 1
    If the underlying operation (like a database query) is asynchronous, then there is NO way to turn that into a synchronous operation and return the value directly from your function. You simply can't. Use promises. See the question yours has been marked a dup of for a full description of your options. – jfriend00 Nov 28 '18 at 16:49

0 Answers0