The condition of while
loop, if it’s while (results2[nextVisit])
, it log nothing
function useTime(i: number, cb: (num: number) => void) {
setTimeout(cb, 1000 * Math.round(Math.random()), i * 2);
}
const total = 10;
const results2 = new Array(total);
let nextVisit = 0;
let countTasks = 2;
for (let i = 0; i < total; i++) {
useTime(i, (result) => {
results2[i] = result;
if (results2[nextVisit] !== undefined) {
console.log(`Tasks ${countTasks}`);
countTasks++;
// If the while loop condition is
// results2[nextVisit], it log nothing
// while (results2[nextVisit]) {
while (results2[nextVisit] !== undefined) {
console.log(nextVisit + "->" + results2[nextVisit]);
nextVisit++;
}
}
});
}