this launches a promise with different times of resolving
those ones are in parallel but the fact of counter
being a refrence that is at its final value when async
works finally does me obtain the same value for res (always 5)
any workaround this ?
//to test dynamics additions based on functions returning
//promises as iterators
var moitabla = document.body.appendChild(document.createElement("table"));
moitabla.setAttribute("id", "mit");
var moitexto = document.createTextNode("to add");
var mirow = document.createElement("tr");
var mitd = document.createElement("td");
mirow.appendChild(mitd);
mitd.appendChild(moitexto);
moitabla.appendChild(mirow);
//here it comes
function* gen() {
let i = 0;
for (/*let must be here or do closure for it */i = 0; i < 5; i++) {
yield(new Promise(function(resolve, reject) {
window.setTimeout(function() {
resolve(i);
}, i * 1000);//end timeout
}/*end function anonymous*/
)//end new promise
); //end promise parse envelper
}; //endfor
}; //end gen
var it = gen();
var bar;
while (!(bar = it.next()).done) {
bar.value.then(function(res) {
console.log("done!! " + res);
});
//always done!! 5 no individual value
};