0

I am at a point in my Node code where I need to retrieve 4 or 5 items from a database (MySQL), then do some math on the results and then return the result to the main part of the code which called the function. I can't really move on in the code until I have the data returned from these function or functions.

Everything I read says to NOT create synchronous functions because you take away all the joy and beauty of using Node. But, I literally can't move on with executing my code without the results from the functions.

So, don't I need a synchronous function here? If so, why does it feel so wrong? LOL.

I thought of doing one big outer function that is synchronous which contains the 4 or 5 functions that actually do the work. I can make the nested functions Async and make the outer function (the container) Synchronous.

Any thoughts on this? New to Node and just trying to do this right the first time.

Fonewiz
  • 2,065
  • 3
  • 17
  • 17
  • 2
    You can't do that. Instead, you should use promises. – SLaks Apr 07 '17 at 17:02
  • node.js literally cannot wait for asynchronous operations to finish before continuing execution. If you have an embedded asynchronous operation inside your function, then that whole function is now asynchronous. You cannot return the value. See [How do I return the response from an asynchonrous call](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) for a discussion of your options. Promises or callbacks will be employed. – jfriend00 Apr 07 '17 at 18:33

1 Answers1

1

Waiting for several i/o operations to finish before continuing is a common problem, especially with node+databases. I try to do as much async as possible and only block when the logical flow of the program absolutely demands it. Your idea of "making the nested function async" seems fine.

You can leverage Promise.all to kick off all the async stuff. If you await the results of the promise, it won't go to the next line of code until the Promise.all is done

Here's a bit of code with silly but descriptive function names that I hope proves helpful.

async function processIAmTryingToDoAsAsynchAsPossible(allCommandsToExecute) {
  try {
    // go get all the data asynchronously and destructure the results. But only move to the next line
    // once all the async calls have returned
    const [firstResult, secondResult, thirdResult] = await Promise.all(allCommandsToExecute.map(eachCommandToExecute => executeSqlFunction(eachCommandToExecute)));

    // assuming it is just math, this is not an i/o operation so it will execute "synchronously"
    const finalMathProduct = doMathOnThisStuff(firstResult, secondResult, thirdResult);

    // assuming this is an i/o operation and the final function returns something you need
    return await functionWeCallAfterGettingTheData(finalMathProduct);
  } catch (err) {
    // You'll get here if the Promise.all has a reject, if the final function errors out,
    // or if you throw an error in one of the other functions.
    throw err;
  }
}
bbuckley123
  • 1,879
  • 13
  • 18