What is the advantage of adding async to a function that returns a promise?

What is the advantage of adding async here?

async function asyncFunc () { return new Promise (function (resolve, reject) { }); } 
+5
source share
2 answers

The only advantage of async is the visual marker, which the function (always) returns a promise, and you don’t even need to scan the body of the function for the return . This can be useful for consistency if you have an async function s string.

In addition: there is absolutely zero gain from him. This is no worse than transferring the return value to an additional call to Promise.resolve() . If the body of your function consists only of a return with a promise (either new Promise or another function call), I recommend not using async .

In general, if your function body does not contain an await expression, you probably also will not need the async . An exception to the rule is when you want to make sure that the function always returns a promise, even if an exception occurs in the code that should lead to the rejection of the promise.

+6
source

I do not think you can use async here if you are not using await inside your promise function.

 async function asyncFunc () { // no await here } 

async/await are shared, and it makes no sense to use one without the other.

+3
source

Source: https://habr.com/ru/post/1274177/


All Articles