I want to do a very simple code, where I have two nested loops in javascript that print out the index they are currently on. (it's the base for something else i want to do). The inner loop has a timer, so it should wait 1 second before printing each number.
I would expect this output:
outer count: 0
inner count: 0
inner count: 1
inner count: 2
inner count: 3
inner count: 4
in out
outer count: 1
inner count: 0
inner count: 1
inner count: 2
inner count: 3
inner count: 4
in out
etc, but this is not happening. Is there some obvious mistake in my code? can async whilst functions even be nested? please help! :(
This is my code so far:
var count = 1;
async.whilst(
function() {return count < 5},
function(callback){
var icount = 0;
console.log("outer count:" + count);
async.whilst(
function () { return icount < 5; },
function (callback) {
console.log("inner count:" + icount);
icount++;
setTimeout(callback, 1000);
},
function (err) {
// 5 seconds have passed
console.log("in out");
}
);
count++;
callback();
},
function(err){
console.log("out out");
}
);
callback
in the asynchronous callback? – Bergi Aug 21 '15 at 1:08