Promises + Generators

In notebook:
FrontEndMasters Async Javascript
Created at:
2016-09-18
Updated:
2016-09-18
Tags:
JavaScript Fundamentals
In the previous example, ​getData​ was calling the iterator when it was done to step through the code.

The inversion of control issue is still present. Someone can call iterator several times, etc.
This will be solved with promises. ​yield promise

We need some plumbing somewhere that drives the generator. It's this code that will get back the promise. ​promise.then(it.next)​ 

Nowadays all major libraries have a runner that does this. ASQ, KOA, etc.
"Bugs happen when you write code that doesn't work like our brains do"This is really a fantastic pattern. 

The next version of JS will have an async function that does exactly this, but you won't need a driver utility to run it. 
Now:
  ASQ.runner(function* (){
  yield ajax(..);
});
After with async
  async function foo(){
  await ajax();
}

foo();
We won't need a new library. This will be the new baseline to write JavaScript. Transpiled code is not great, but for sure at least as good, but probably 100 times better than the crappy code you would write.These are not polyfills but transpiled. Babel supports this.Sidetone. The concept of cancellable promises comes from this pattern. In fact above is this: ​foo().then(..)​.
If we need a way to abort ​foo​. So that make promises cancellable.
He thinks it a very very bad idea. 
Generators return iterators, so you have control from the outside. 
So this is why he will keep using generators. Because you keep the control from the outside (like cancelling it). Most of the times he will keep using generators and not async functions.

ASQ supports async  functions. You can pass it an async function or generator.