0

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");
        }
        );
1
  • Call callback in the asynchronous callback? Commented Aug 21, 2015 at 1:08

2 Answers 2

5
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.

Sign up to request clarification or add additional context in comments.

1 Comment

i have found a bug in this answer i put here fix code :
0

Nested loops using async.js :

    var i = 0;
    async.whilst
    (
        function() {return i < 3},

        function(next_i)
        {
            var j = 0;
            async.whilst
            (
                function () { return j < 3; },
                function (callback) 
                {
                    console.log( i +' / '+ j);
                    j++;
                    setTimeout(callback,0);
                },
                function () 
                {
                    i++;
                    next_i();
                }
            );
        }
    );

console.log

0 / 0
0 / 1
0 / 2
1 / 0
1 / 1
1 / 2
2 / 0
2 / 1
2 / 2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.