I have some node.js code like that
console.log("diagnostic information");
nasty_stuff_fails_now();
Since console.log is asynchronously executed but "nasty_stuff_fails_now()" fails before the next tick of the event loop I never see the diagnostic information. How can I push the output to the console before nasty_stuff_fails_now() gets executed? Ideally I would like to synchronously push to the console. The performance penalty is acceptable during debugging.
I would argue that it makes more sense, both semantically and for logic-wise, for you to use a try catch.
try {
nasty_stuff_fails_now();
} catch( exception ) {
//log diagnostic for failure
}
The console.log is atomic, so it should be printing everything before running the code that fails. If you are having a custom log function, that actually does async things. Then I would suggest you to return a promise.
logMyAsyncStuff().then( nasty_stuff_fails_now() );