Asyncaha — Awaitashyah.

A Famous JSvilla Legend says that
If one wants to be familiar with a framework/function , go through it’s docs,
but if one wants to be an expert, plough through each and every tiny bit of it’s source code, tear it down, assemble and then write a clone of your own.
Ok enough of all this. Let’s get our hands dirty on one of the most popular and refined JS feature — Async Functions.

We know ,everything in this world is Asynchronous. Infact no one waits for others. Let us define two of the most common asynchronous tasks in our daily life.
Action 1: Turn on the tap for water.
Action 2: Start brushing your teeth.
But on one D-day 
Your household runs out of water and you continued with this async action -
“Start brushing your teeth”. So you are doomed.
We refer to the JS Monks who give us one very special hidden(not anymore) genjutsu to overcome such scenarios.

AWAIT and IT shall be Given to YOU
That’s it. This is the secret ingredient we have been searching for. We will await “Turn on the tap for water” and then 
Start brushing your teeth

function startBrush(){… } , function turnOnTap(){…} 
async function dailyActivity(){
 const tapStatus = await turnOnTap();
//If tapStatus if good then go on
const brushStatus = await startBrush();
Well life was never so easy. But oh oh, wait , we could see some rebellion outside our JSVilla. Oh wait, these are the followers of Promises.

These are some goons from PromiseVilla, a house which earlier sought pledge to JSVilla but now gravely disappointed with this new feature. Again we resort to our monk for this crisis.

Monk: Ha Ha, is this the reason about all this fuzz ? Let me reveal
Generators + Promises = Async , Await.

Oh. Is that it ?. The greatest heist of JSVilla has been resolved. Just like Naruto inherits from 4th Hokage and Jirayia. Async Await are blendage of promises and generators. Won’t believe this ? watch it as we start making you walk through this code.

Generator Recap : 
 Enter the Generator : Yield until you cannot yield more.
function* fruitFactory(){
 yield “apple”;
 yield “mango”;

var fruitFn = fruitFactory();
var myFirstFruit =; //apple

-> Now i can yield the next fruit by simply yielding once more.
var my secondFruit =; //mango
//Now it can’t be yielded any more.

Oh so Simple then, 
all i need to do is to write something like this to make it work like ASYNC AWAIT.

function * dailyAction(){
 var tapStatus = yield
var brushStatus = yield startBrush();

Life is never so easy- Monk of JSVilla
Oh then we must be doing something silly. Oh wait , yeah turnOnTap and startBrush are ASYNC actions(promises here). We awaited them but we need to make them behave like promise otherwise all we get is UNRESOLVED (pending) promises. 
Let us now write a helper function that would take care of all these generator functions which have async actions.

All we need is a runner. A cool runner. 
Just like a bob sledge in Jamaica Bob sledge team from the movie Cool Runnings.

We need to create a runner function that would await until all promises are resolved. We need our first player(turnOnTap) to get onboard and then (brushTeeth).

Here’s a sample ASYNC AWAIT+PROMISE runner function.

var asyncRunner=(generatorFunction)=>{
Step 1 : First get reference to the generatorFunction template
var genFunctionGenerator = genFunction();

Step 2: runGenerator();
function runGenerator(val){
 var promiseResult=;
 Step 3 : Now keep on checking whether this will resolve or not.
 return promiseResult.value; 
 // wait for promise resolve and if resolved recursively yield it again
//Step 4 : Oh that’s it . Promise is resolved. Now we may yield it again. runGenerator(results[0]);

//Just run to have it a behaviour similar to ASYNC AWAIT.
var asyncAwaitGenerator = asyncRunner(asyncAwait);

Yippiee. Peace has been restored in JSVILLA again. 
PromiseVilla and GeneratorVilla swore their allegiance till end to JSVilla again.

Refer this gist file for a working implementation.

And finally Arigatou Gozaimashu. :) : ) :)