Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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");
        }
        );
share|improve this question
    
Call callback in the asynchronous callback? – Bergi Aug 21 '15 at 1:08
up vote 3 down vote accepted
var count = 1;
        async.whilst(
        function() {return count < 5},
        function(outer_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");
                    outer_callback(); // <--- here
                }
            );

            count++;


        },
        function(err){
            console.log("out out");
        }
        );

async.whilst is not blocking, which means that all 5 of them are scheduled to run simultaneously.

The change I made is that now in the code the outer loop iteration is only completed as inner loop iteration is done.

share|improve this answer
    
thank you so much, this worked perfectly :) – cmperezg Aug 21 '15 at 16:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.