I am looking for a answer on what to use in my nodeJS app.
I have code which handles my generic dB access to mssql. This code is written using an async
Actually it depends on your node version, But if you can use async/await
then your code will be more readable and easier to maintain.
When you define a function as 'async' then it returns a native Promise
, and when you call it using await it executes Promise.then.
Note:
Put your await calls inside a try/catch
, because if the Promise fails it issues 'catch'
which you can handle inside the catch block.
try{
let res1 = await your-async-function(parameters);
let res2 = await your-promise-function(parameters);
await your-async-or-promise-function(parameters);
}
catch(ex){
// your error handler goes here
// error is caused by any of your called functions which fails its promise
// this methods breaks your call chain
}
also you can handle your 'catch' like this:
let result = await your-asyncFunction(parameters).catch((error)=>{//your error handler goes here});
this method mentioned does not produce an exception so the execution goes on.
I do not think there is any performance difference between async/await
other than the native Promise module implementation.
I would suggest to use bluebird
module instead of native promise built into node.