adriaandotcom / blog.adriaan.io

The tech blog of Adriaan
https://blog.adriaan.io
Other
10 stars 14 forks source link

anti-pattern? #13

Closed hrivera2014 closed 4 years ago

hrivera2014 commented 5 years ago

I was having a similar issue then I got to a stackoverflow question:https://stackoverflow.com/questions/43036229/is-it-an-anti-pattern-to-use-async-await-inside-of-a-new-promise-constructor So your code can be write so:

async function returnSomething(name) { const somethingElse = await returnSomethingElse(); return new Promise( (resolve, reject) => { resolve(somethingElse); }); } What you think guys.

Thinking a little more. What about if there are more than one asyncronous activity that need to be waited inside returnSomething function. Then I dont know how to treat these case.

adriaandotcom commented 5 years ago

If returnSomethingElse errors, it will crash the app. There need to be a Promise or try...catch block around it. Also returning a resolve of a Promise alone can be written as:

return Promise.resolve(somethingElse);

But I don't see a point why you would not await in a Promise.

Marcinthedev commented 4 years ago

Problems with error handling ? https://eslint.org/docs/rules/no-async-promise-executor

adriaandotcom commented 4 years ago

Old issue.