What is the benefit of prepending async here?
async function asyncFunc () {
return new Promise (function (resolve, reject) {
});
}
What is the benefit of prepending async here?
async function asyncFunc () {
return new Promise (function (resolve, reject) {
});
}
The only benefit of async
is as a visual marker that the function will (always) return a promise, and you don't even have to scan the function body for the return
statement. It might be useful for consistency if you have a row of async function
s.
Apart from that: there's absolutely zero benefit from it. It's as good as wrapping the return value in an additional Promise.resolve()
call. If your function body only consists of a return
statement with a promise (either a new Promise
or another function call), I recommend not to use async
.
In general, if your function body does not contain an await
expression, you probably don't need the async
keyword either. The exception from the rule is when you want to make sure that the function always returns a promise, even if there's an exception raised in the code which should lead to a promise rejection.
I don't think there is any benefit of using async
here unless your are using await
inside your promise
function.
async function asyncFunc () {
// no await here
}
async/await
are used in conjunction and there is no point of using one without other.