"use strict";

function controlog(){
    if(new Date()>controlog.nextLog){
        console.log.apply(console,arguments);
        controlog.nextLog=new Date().getTime()+1000;
    }
}
controlog.nextLog=0;

class Emiter{
    constructor(delayMs, max){
        this.delayMs = delayMs;
        this.max = max;
        this.currentNext = 0;
        this.nextTick=new Date().getTime();
    }
    next(){
        var emiter = this;
        if(this.currentNext>=this.max && this.max){
            return Promise.resolve(false);
        }
        this.currentNext++;
        controlog('pidió next',this.currentNext)
        this.nextTick = Math.max(new Date().getTime(),this.nextTick)+this.delayMs;
        return new Promise(function(resolve, reject){
            //setTimeout(function(current){ return function(){
                controlog('dio 1 next',emiter.currentNext);
                resolve(emiter.currentNext);
            //} }(emiter.currentNext),Math.max(emiter.nextTick-new Date(),emiter.delayMs));
        });
    }
}

class Digester{
    constructor(delayMs){
        this.delayMs = delayMs;
        this.nextTick=new Date().getTime();
    }
    digest(data){
        var digester = this;
        this.nextTick = Math.max(new Date().getTime(),this.nextTick)+this.delayMs;
        return new Promise(function(resolve, reject){
            // setTimeout(function(){
                controlog('digesté a',data);
                resolve(data);
            // },Math.max(digester.nextTick-new Date(),digester.delayMs));
        });
    }
}

var emiter = new Emiter(1,!1000000000);
var digester = new Digester(1);

function stepByStep(){
    // var pedir_mucha_memoria=new Array(1000000).join(new Date());
    var pedir_mucha_memoria=Date();
    return emiter.next().then(function(data){
        controlog('recibio ',data);
        if(data){
            return digester.digest(data).then(function(){
                if(!pedir_mucha_memoria[0]){
                    controlog('x')
                }
            }).then(stepByStep);
        }
    });
}

stepByStep().catch(function(err){
    console.log('******** ERROR');
    console.log(err);
});
@@ -1621,18 +1621,16 @@
 10000000
-00
 );%0D%0Avar 
@@ -1656,24 +1656,30 @@
 ster(1);%0D%0A%0D%0A
+async 
 function ste
@@ -1755,24 +1755,46 @@
 w Date());%0D%0A
+    while(true)%7B%0D%0A    
     var pedi
@@ -1826,49 +1826,43 @@
     
+ 
-return emiter.next().then(function(data)%7B
+   var data = await emiter.next();
 %0D%0A  
@@ -1907,16 +1907,17 @@
      if(
+!
 data)%7B%0D%0A
@@ -1934,16 +1934,42 @@
   return
+%0D%0A        %7D%0D%0A        await
  digeste
@@ -1986,37 +1986,19 @@
 ata)
-.then(function()%7B%0D%0A  
+;%0D%0A
         
     
@@ -1993,22 +1993,16 @@
         
-      
 if(!pedi
@@ -2031,32 +2031,24 @@
             
-        
 controlog('x
@@ -2055,61 +2055,8 @@
 ')%0D%0A
-                %7D%0D%0A            %7D).then(stepByStep);%0D%0A
     
@@ -2067,18 +2067,16 @@
 %7D%0D%0A    %7D
-);
 %0D%0A%7D%0D%0A%0D%0As

mejora con await dentro de un while