With ES2017 JavaScript got a feature called async-functions. They are a handy feature to streamline your asynchronous code a bit more.
"But Kay, I...
For further actions, you may consider blocking this person and/or reporting abuse
It happens often that an asynchronous function takes more than one (earlier computed) asynchronous value. With async/await, all the async function calls are done within the same function scope, exposing the values within that scope, and therefore composing those values with other asynchronous functions requires much less boiler plate.
Using a promises-only approach, or callbacks only, or something like the
async
library will always result in a lot more boiler plate to have access todataString
, e.g. promises-only:First, I liked the "everything is asynchronous" way of working of Node.
Then, I loved Promises because it made async code a first-class citizen.
Now, I'm just completely and absolutely sold on async/await and won't change it for anything.
Seriously, with Promises I could maybe accept they were glorified callbacks in most situations. They're really not, but well, ok, you like callbacks, that's fine. But with await, oh my god, that's another world. I used to write functions to emulate while loops with promises, but now? now you can write loops with promises!! It's awesome.
So yeah, thank you for this article, more people need to know the goodies of async/await.
I found observables more sound, because they make everything work like an array on steroids.
But I have to admit, abstracting async behavior behind an array-like idea is a bit much to swallow for the regular dev. Making it accessible via constructs like loops is much easier to grasp.
I would just add for the last example that Node 8 also includes a promise utility you can use to write:
Great post! You can also use async-await syntax with TypeScript