javascript - Node blocking function containing nested NON blocking functions? -


i @ point in node code need retrieve 4 or 5 items database (mysql), math on results , return result main part of code called function. can't move on in code until have data returned these function or functions.

everything read says not create synchronous functions because take away joy , beauty of using node. but, literally can't move on executing code without results functions.

so, don't need synchronous function here? if so, why feel wrong? lol.

i thought of doing 1 big outer function synchronous contains 4 or 5 functions work. can make nested functions async , make outer function (the container) synchronous.

any thoughts on this? new node , trying right first time.

waiting several i/o operations finish before continuing common problem, node+databases. try async possible , block when logical flow of program absolutely demands it. idea of "making nested function async" seems fine.

you can leverage promise.all kick off async stuff. if await results of promise, won't go next line of code until promise.all done

here's bit of code silly descriptive function names hope proves helpful.

async function processiamtryingtodoasasynchaspossible(allcommandstoexecute) {   try {     // go data asynchronously , destructure results. move next line     // once async calls have returned     const [firstresult, secondresult, thirdresult] = await promise.all(allcommandstoexecute.map(eachcommandtoexecute => executesqlfunction(eachcommandtoexecute)));      // assuming math, not i/o operation execute "synchronously"     const finalmathproduct = domathonthisstuff(firstresult, secondresult, thirdresult);      // assuming i/o operation , final function returns need     return await functionwecallaftergettingthedata(finalmathproduct);   } catch (err) {     // you'll here if promise.all has reject, if final function errors out,     // or if throw error in 1 of other functions.     throw err;   } } 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -