Useful reading to understand the Promises, Generators and the async/await feature for Node.js/JavaScript

; Date: Sun Nov 20 2016

Tags: Node.JS »»»» Asynchronous Programming

The long-awaited async/await feature for JavaScript promises to make our lives much easier. Instead of asynchronous JavaScript programming being tricky and error-prone, with async/await our code will look like regular synchronous code we write in other languages, but will accommodate asynchronous code execution while retaining the single-threaded nature of JavaScript. With this feature we declare async functions, and within their boundary magic occurs - we can put the "await" keyword in front of a function which produces a Promise, and automagically JavaScript will wait for the Promise to resolve and give us the result or else throw the error. What follows is a list of posts describing how to use async/await.

Also included are links to posts about Promises and Generators. Together the three give us a rich tapestry of capabilities to simplify asynchronous programming, make our code look cleaner and more robust.

(medium.freecodecamp.com) Write Modern Asynchronous Javascript using Promises, Generators, and Coroutines Shows several ways to use Generators in an Express application. He suggests that since co(function* () { }) is so similar to async functions, and much more widely available, that we should start using those today.

(www.2ality.com) Tips for using async functions (ES2017) An excellent tour of best practices for using async functions.

(www.2ality.com) ES proposal: asynchronous iteration A proposal (don't get too excited yet) for asynchronous looping structures.

(www.2ality.com) Three useful Babel presets Lists several useful Babel tools to enable certain experimental features in JavaScript, including the async/await feature.

(github.com) promise-sequential "Like Promise.all, but sequentially"

(davidwalsh.name) JavaScript Promise API An excellent tour of the Promise object and the best practices for its use.

(scotch.io) Understanding JavaScript Promises, Pt. I: Background & Basics A good introduction to Promise's.

(medium.com) Using ES6 array destructing with Promises

(medium.com) The Hidden Power of ES6 Generators: Observable Async Flow Control An in-depth look at Generator functions.

(medium.com) Retrofitting Node/NPM Packages with Async/Await. Rewriting your code to use async/await

(medium.com) Error handling in long Promise chains "The longer the chain the easier it is to break it"

(medium.com) Promises, Promises (or how not to lose your marbles) Some best practices for using Promise's.

(ponyfoo.com) Understanding JavaScript’s async await

(stackoverflow.com) Installing Babel V6.x ES7 Async/Await on Node.js v6.2.0 with Nodemon A Stack Overflow question about using Babel to transpile async/await

(codequs.com) Using ES7 Async/Await in Node.js & Babel.js

(stackoverflow.com) Get the value of a Javascript Promise in a synchronous way An excellent discussion of why we need async/await to simplify asynchronous programming in JavaScript

(blog.shaunxu.me) ES7 Async/Await in Node.js with Babel.js

(tc39.github.io) https://tc39.github.io/ecmascript-asyncawait/ - Home page for the committee developing async/await for JavaScript

(hackernoon.com) Node.js Tip - Minimal Babel with Node v6 The precise minimum steps required to setup Babel with Node.js 6.12 to use async/await.

About the Author(s)

(davidherron.com) David Herron : David Herron is a writer and software engineer focusing on the wise use of technology. He is especially interested in clean energy technologies like solar power, wind power, and electric cars. David worked for nearly 30 years in Silicon Valley on software ranging from electronic mail systems, to video streaming, to the Java programming language, and has published several books on Node.js programming and electric vehicles.

Books by David Herron

(Sponsored)