I am developing some Nodejs scripts and I have a question about the usage of async keyword in JS itself.
For example, consider that I have a function which returns a promise like this:
function myFunction () {
... some job
return new Promise(async (resolve, reject) => {
const innerResult = await someHeavyJob();
... some job
resolve(innerResultThatIsManipulated);
});
}
And I can write it in another way of this:
async function myFunction () {
... some job
const innerResult = await someHeavyJob();
... some job
return innerResultThatIsManipulated;
}
But I have a question that if I am choosing the first solution, then am I allowed to use async
keyword and is that recommended or not? For example, something like this:
async function myFunction () {
... some job
return new Promise(async (resolve, reject) => {
const innerResult = await someHeavyJob();
... some job
resolve(innerResultThatIsManipulated);
});
}
To emphasize that this is an async method and is returning promise (not a simple object) in all paths of its code.
So, what is the recommended way to use?