# JavaScript Async/Await and ES6 Generators for Asynchronous Programming

DevFeed: [JavaScript Async/Await and ES6 Generators for Asynchronous Programming](<https://devfeed.tech/articles/javascript-from-callback-hell-to-heaven-31999.md>)

Original publisher: [Read original article](<https://tech.finn.no2015/10/16/javascript-from-callback-hell-to-heaven/>)

Author: Tor Arne Kvaløy

Published: 2015-10-16T14:00:00Z

Content type: tutorial

Language: en

Sources: [Finn.no](<https://devfeed.tech/sources/finn-no.md>)

Topics: [JavaScript](<https://devfeed.tech/topics/javascript.md>), [async/await](<https://devfeed.tech/topics/async-await.md>), [async](<https://devfeed.tech/topics/async.md>), [function](<https://devfeed.tech/topics/function.md>), [Code](<https://devfeed.tech/topics/code.md>), [JSON](<https://devfeed.tech/topics/json.md>)

Tags: [async](<https://devfeed.tech/tags/async.md>), [asynchronous](<https://devfeed.tech/tags/asynchronous.md>), [await](<https://devfeed.tech/tags/await.md>), [callback](<https://devfeed.tech/tags/callback.md>), [es6](<https://devfeed.tech/tags/es6.md>), [function](<https://devfeed.tech/tags/function.md>), [javascript](<https://devfeed.tech/tags/javascript.md>), [json](<https://devfeed.tech/tags/json.md>), [node](<https://devfeed.tech/tags/node.md>)

## AI overview

This tutorial explains how JavaScript callbacks and nested Promise chains make asynchronous code cumbersome, then shows how ES7 async/await can express asynchronous operations in a more synchronous style. It also describes the role of ES6 generators and demonstrates reading and parsing a JSON file with Node.js.

## Source excerpt

This blogpost explains how some nifty features in ES7 will make it easier to write asynchronous code, and how ES6 generators will pave the way for this. Async/await My main annoyance with javascript and Node has been the tedious asynchronous programming model of callbacks, leading to nested callbacks and the so called "callback hell" or "pyramid of doom". Take for example the following code where we are creating a function that reads a file and parses it to JSON, and see how cumbersome it is to read and follow the code: const fs = require("fs"); function readJSONFile(callback) { fs.readFile("file.json", "utf8", (err, data) => { if (err) return callback(err); let json; let parseError; try { json = JSON.parse(data); } catch(e) { parseError = e; } callback(parseError, json); }); }; readJSONFile((err, json) => { if(err) { //handle error } console.log(json); //continue program }); This was slightly improved and simplified with ES6 promises, however, as we can see in the following code, we are still stuck with nested .then-calls: const bluebird = require("bluebird"); const fs = bluebird.promisifyAll(require("fs")); function readJSONFile() { return fs.readFileAsync("file.json", "utf8") .then(data => { return JSON.parse(data); }) }; readJSONFile() .then(json => { console.log(json); //continue program }) .catch(err => { //handle error }) Callbacks and nested .then-statements will be a thing of the past with the upcoming version of Javascript (ES7). It will provide us with the keywords async and await, which will enable us to write asynchronous code as we would have written it in an imperative synchronous way: const bluebird = require("bluebird"); const fs = bluebird.promisifyAll(require("fs")); async function readJSONFile() { const data = await fs.readFileAsync("file.json", "utf8"); return JSON.parse(data); } async function() { try { const json = await readJSONFile(); console.log(json); //continue program } catch(err) { //handle error } }; The await keyword can either take a