Menu

React, etc. Tech Stack

React, Flux, GraphQL, Hack, HHVM...? All of this and more!

Stop worrying about generators and embrace Async/Await in JavaScript using TypeScript

JavaScript has always been asynchronous in nature. From the browser events since the 1990s to the event loop driving many web services' backend code run in asynchronous nature. JavaScript itself had pretty bad support for writing sane asynchronous code.

With ES6 we JavaScript developers to work with promises and generators instead of callbacks. This was and is a big improvement, but this is still somewhat complicated. This lead to alpha-male developers going time after time to preach about the bliss of generators and their virtues.

The point of getting results from a program was often secondary to how elegant it supposedly was to code. In the more mature .NET world developers have had asynchronous coding for a long time in C#. They've had better tools for this too, with Async/Await being an old timer in C#.

EcmaScript 7 will bring Async/Await to JavaScript. This will make it easy to write asynchronous code that looks a lot like synchronous code with the async and await keywords:

async function doAsyncOp () {
    var val = await asynchronousOperation();
    console.log(val);
    return val;
};

ES7 is only in the process of being release, but even before that you can start using this method with TypeScript, an Open Source language that extends JavaScript. It comes with excellent tooling for developers allowing transpiling to lower versions of JavaScript through transpiling.

Written by Jorgé on Wednesday May 11, 2016

Permalink -

« JavaScript back to basics: You might not need React or Angular 2 - Facebook technology powers WordPress and Drupal with React.js and GraphQL »