DEV Community

Discussion on: About async functions

Collapse
 
tkdodo profile image
Dominik D

it's multiple things here really:
1) combining async/await with .then(). I think you'd want to choose one way and then stick to it
2) awaiting the last (and in this case only) Promise is unnecessary. You can just return the Promise, instead of awaiting it and then returning a new Promise due to the nature of an async function.
3) Since the await is unnecessary (see point 2), unless you get rid of the the .then() chain (see point 1), the function being async is also unnecessary.

All in all, that combination is just unnecessarily verbose and shows that whoever has written it doesn't understand what async functions are really doing :)

Collapse
 
alecvision profile image
alecvision • Edited

I'll be doing a lot more reading up on all of this now that I think I'm scraping at what you're saying...

My code has consistently worked up to a recent project - I wrote a function to update state which returns successfully but doesn't produce the desired side-effects. I'm almost certain now it has to do with an unresolved promise somewhere.

I actually scrapped it and chose a different approach, fearing it might be an API bug with the component since I had successfully implemented a similar solution elsewhere in my app.

Thanks for your post and reply - I'm just now getting to the level of confidence to even reach out to other developers, so I really appreciate your thoughtful response!

EDIT: I took one 'await' out of my init function just to see if I was understanding things right - and it still works, but faster by a factor of 3! Thanks again :D

Thread Thread
 
tkdodo profile image
Dominik D

Just saw your edit and that’s incredible πŸ™Œ