function *asynLoader(list){
for( let data of list){
yield new Promise( (resolve, reject) => resolve(data) );
}
}
function queueLoader(generator, completeHandler){
let {done, value} = generator.next();
if( ! done){
value.then( data => {
completeHandler(data);
queueLoader(generator, asyncLoader_completeHandler);
});
}
}
function asyncLoader_completeHandler(data){
console.log(data);
}
let generator = asynLoader([0, 1, 2]);
queueLoader(generator, asyncLoader_completeHandler);
function *getData(array){
for( let data of array){
yield new Promise( (resolve, reject) => resolve(data) )
}
}
function complete(generator){
let {done, value} = generator.next();
if( ! done){
value.then( data => {
console.log(data);
complete(generator);
});
}
}
let generator = getData([0, 1, 2]);
complete(generator);