Promises vs Async Await

Too may times within the last few weeks have I gone over some JS code and seen Async Await syntax. Even in some Github readme docs, code examples occasionally use it too.

For a while, I thought that Promises were fine and Async Await (which I will now abbreviate as “AA”) was unnecessary, but I’m now convinced that for everyday use, AA is a great alternative- but only if you go all in.

Before (using Promises):

After (using Async Await):

There are pretty much 2 differences to note:

1.) The surrounding function must be declared as an async function.

2.) The evocation of the promise function must be prepended with await

It makes async code look synchronous and can vastly enhance readability of async code without worrying about callbacks.

Drawbacks to Async Await? Inconsistency. You can’t just replace usage of then with usage of await – you have to also make the surrounding function async. So if you want consistency, you’d either need to make all functions async or use traditional then syntax… or do what most humans would do and just deal with incosistency.

A further question would be: when should you use Promises and when not to? Should promises only be reserved for async code? If so, it may become a pain to figure out how a function should be evoked. I’ve experimented with “always use AA for both sync and async functions” and found it to be a bit of boilerplate to get going, but quite pleasant to use.

Leave a Reply

Be the First to Comment!