By combining Promises and Generators, we can create a function that handles asynchronous tasks (let's call it
async). This function takes one parameter - a generator function. Each
yield statement in the generator returns a promise, which calls the generators iterator once resolved. Therefore each asynchronous task in the
async function moves on to the next task in sequential order. Let's look at some pseudocode:
This code lacks error handling and other technicalities present in production code, but displays the basic idea of the
async function. The following pseudocode uses
async() to execute HTTP GET requests.
With the reusable
async function, asynchronous code is very simple! Even better, its written in a synchronous manner with no promises or dreaded callbacks to deal with! All of the
Promise code is abstracted away. So why am I only showing pseudocode and not the real thing? The reason is because ECMAScript 2017 (or following the old naming convention ES8) implemented this pattern using the new
In my discovery on promises I created a google search API for cat posts. Let's refactor this example using an ES2017
async function. I used Node 9 for the refactor to avoid transpiling my code with Babel. One thing that Node 9 (the newest version of Node.js as of November 2017) does not support by default is ES6 modules. As a workaround, the
--experimental-modules flag can be used to enable ES6 modules2. To notify Node.js that files are modules, the .mjs file extension is used. These files are hilariously referred to as Micheal Jackson Scripts. Hopefully the .mjs files are just for the experimental version and not here to stay - I would miss those .js files (and it doesn't seem like an elegant solution)!
Now let's get to the code. The first module creates a promise for a request to the google search API3. The second module imports the first module and uses the ES2017
You may have noticed that the
await keywords are simply syntactic sugar for our
await() functions. This is a truly elegant solution that allows asynchronous code to be written in an easily understandable synchronous manner. Executing this code results in the following:
async function is very powerful! Although you never need to fully understand the details of combined Promises and Generators when using
async functions, it is still good to know what is going on behind the scenes! All the code from this discovery is on GitHub.