Meta js

Welcome to my rant

Before diving into deep thoughts

Warnings
Purely experimental
Diabolical in nature
No "Public" documentation
Don't use in production 
I repeat, don't use in production
About
Generators
Coroutines
may be Proxy
may be Symbols 
Types of functions​​

?

Generators
function* willItRunForever() {
  let count = 0;
  while(true) {
    yield;
    console.log(count);
    count++
  }
}
Hello World
function* Hello() {	
  yield 1;
  
  console.log('exiting');
}
How do i call this?

 

It's not your normal function.
Call to `hello()` returns object. YES object!!!
WHAT the function?
Well,
function* Hello() {	
  yield 1;
  console.log('exiting');
}
// gen is a object, object, object
const gen = Hello();
// to acutally make run the 
// function upto the first `yield`.
// returns object { value : 1, done: false}
// `done: false`  means function is not done
const result = gen.next();
// to run the rest of the statements in function
gen.next();
// done
function* MoreHello() {	
  yield 1;
  yield 2;
  console.log("done");
}
// YUP
const gen = MoreHello();
// upto first yield
const result = gen.next(); 
console.log(result.value, result.done) // ??
// upto second yield
const again = gen.next();
console.log(result.value, result.done) // ??
// upto `return`
const last = gen.next();
console.log(result.value, result.done) // ??
// done
LEVEL 1
function* forever() {	
  let count = 0;
  while(true) {
    yield count;
    count++
  }
}
Infinite Counter
LEVEL Fibonacci
function* fib() {
  let a = 0,
      b = 1;
  while(true) {
    yield a;
    temp = a;
    a = b;
    b = temp + b;
  }
}
Any THOUGHTS ?
function* foo() {
  console.log("FIRST call");
  yield 1;
  console.log("SECOND call");
  yield 2;
  console.log('PLEASE STOP');
}
Caller takes a control 
back
Thoughts?
Why control is given back to caller 
Why can't my function execute without being paused and just freaking run until function ends?
everytime it hits `yeild`?
Why would anyone think 
of such function that pause?

I don't use but who the f uses it ?

DEEP Thoughts?
Where am i going?
Turns out your event loop uses it all the time
But HOW?
Imagine Me
as a event loop
and watch deeply
DEMO TIME......
TASK 1: Even Counter
TASK 2: Odd Counter
Event loop
Output
Even Task runs and
Odd Task runs and
Even Task runs and
Odd Task runs and
Even Task runs and
Odd Task runs and
Even Task runs and
Odd Task runs and
Even Task runs and
Control is back to caller(event loop).
Odd Task runs and
Control is back to caller(event loop).
AGAIN
Even Task runs and
.........
yield

 

 

REAL LIFE
TASK 1
TASK 2
Tasks run one after another.

It's Asynchronous.

It's Concurrent.

But not parallel

TAKE AWAY
JS deals multiple things at the same time but does not run multiple things at the same time.

Concurrent but not parallel.

OKAY
That's cute.

Async/Await uses generators.

Is there is any other uses of generators?
WELL
As a Context manager
As a State Machine
Notice Something?
As a Iterator
As a Tree/Graph traversal
Notice Something?
As a zero memory allocated data transormer
Basically, 
Sky is the limit
THESE DAYS
DEEP DIABOLICAL EXPERIMENT WITH COROUTINES
building GC friendly parallel stream processing compute pipeline 
Quick tour (SMALL SCALE)
Number Parser NODE
Odd filter NODE
Odd filter NODE
Prime filter NODE
Source
Number
Parser
Odd
Filter
Prime
Filter
Sink
OR?
FILE
Number parser
Even
Odd
Prime
<100
>100
Socket
Console
FANOUT? 
OR?
Parallalized??

USING DIFFERENT EXECUTOR ENGINE

Made with Slides.com