Skip to content

Instantly share code, notes, and snippets.

@akumpf
Last active December 11, 2015 00:09
Show Gist options
  • Save akumpf/4514984 to your computer and use it in GitHub Desktop.
Save akumpf/4514984 to your computer and use it in GitHub Desktop.
// Single for loop (i)
// i loop
loop(0, function(i){return i<10;}, function(i){return i+1;},
function(i, next){
// looping.
console.log(i);
next();
},
function(i){ console.log("all done."); }
);
// 2-Level deep for loop (i,j)
// i loop
loop(0, function(i){return i<10;}, function(i){return i+1;},
function(i, next){
// j loop
loop(0, function(j){return j<10;}, function(j){return j+1;},
function(j, next){
// looping.
console.log(i,j);
next();
},
function(i){ next(); } // note: this next is the parent's next. yay!
);
},
function(i){ console.log("all done."); }
);
// 4-Level deep for loop (i,j,k,m)
// i loop
loop(0, function(i){return i<5;}, function(i){return i+1;},
function(i, next){
// j loop
loop(0, function(j){return j<5;}, function(j){return j+1;},
function(j, next){
// k loop
loop(0, function(k){return k<5;}, function(k){return k+1;},
function(k, next){
// m loop
loop(0, function(m){return m<5;}, function(m){return m+1;},
function(m, next){
// --
// looping.
console.log(i,j,k,m);
next();
// --
},
function(m){ next(); } // note: parent's next.
);
},
function(k){ next(); } // note: parent's next.
);
},
function(j){ next(); } // note: this next is the parent's.
);
},
function(i){ console.log("all done."); }
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment