DEV Community

JavaScript Awaits

K on July 02, 2017

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...
Collapse
 
godspeedelbow profile image
Eelke Boezeman

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.

async function f() {
    const dataString = await getServerData();
    const parsedData = await parseData(dataString);
    const filteredData = await filterData(parsedData, dataString); // <-- scoped access to dataString
}
Enter fullscreen mode Exit fullscreen mode

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 to dataString, e.g. promises-only:

function f() {
  getServerData()
  .then(dataString => [dataString, parseData(dataString)]), // <-- pass dataString along
  .then([dataString, parsedData] => filterData(parsedData, dataString)) // <-- so you can access it here
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
danielescoz profile image
Daniel Escoz

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.

Collapse
 
kayis profile image
K

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.

Collapse
 
amorgaut profile image
Alexandre Morgaut • Edited

I would just add for the last example that Node 8 also includes a promise utility you can use to write:

const { promisify } = require('util');
const fs = require('fs');
const readFileAsync = promisify(fs.readFile);

async function f() {
  try {
    const data = await readFileAsync('README.md');
    if (await validateOnServer(data)) {
      console.log("File OK");
    }
  } catch (e) {
    console.error(e);
    return;
  }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ssalka profile image
Steven Salka

Great post! You can also use async-await syntax with TypeScript