---
title: "ES7 Async Await"
description: "The async/await proposal that changed how we handle asynchronous JavaScript. Practical notes and code examples on ES7 Async."
url: "https://h3manth.com/scribe/es7-async-await/"
canonical_url: "https://h3manth.com/scribe/es7-async-await/"
markdown_url: "https://h3manth.com/scribe/blog/es7-async-await.md"
publishDate: "2015-06-21T00:00:00.000Z"
author: "Hemanth HM"
category: "javascript"
tags: ["javascript","es7","async"]
word_count: 193
reading_time_minutes: 1
x_markdown_tokens: 405
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# ES7 Async Await

> The async/await proposal that changed how we handle asynchronous JavaScript. Practical notes and code examples on ES7 Async.

[async/await](http://tc39.github.io/ecmascript-asyncawait/) of ES7 is one of my favorite proposals, that would for sure change the way we handle asynchronous code in JavaScript!

## Promise Example

```javascript
let p1 = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve('Promise 1 ');
    }, 30);
  });
}

let p2 = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve('Promise 2');
    }, 10);
  });
}

async function getVal() {
  var res1 = await p1();
  var res2 = await p2();
  return res1 + res2;
}

(async function() {
  var res = await getVal();
  console.log(res); // Promise 1 Promise 2
}());

## File System Example
javascript
var lieFS = require('lie-fs'); // Promisified FS

(async function() {
  var list = await fs.readdir('~/');
  console.log(list);
}());

## Chain Animation (From the Spec)
javascript
async function chainAnimationsAsync(elem, animations) {
  var ret = null;
  try {
    for (let anim in animations) {
      ret = await anim(elem);
    }
  } catch(e) {
    /* ignore and keep going */
  }
  return ret;
}

## Async Await Fetch
javascript
let data = await (await fetch(url)).json()
```

Do let me know if you have your own set, until then happy async/await!
