node package manager
Don’t reinvent the wheel. Reuse code within your team. Create a free org »

loops

node-loops: Basic, non-blocking loop functions for NodeJS

This project aims to provide a simple syntax for the basic loop operations, allowing them to be performed in a non-blocking fashion. Each iteration occurs on a process tick, which allows other operations, such as timers and socket listeners, to occur during a long loop

Basics

var Loops = require('loops');

This provides the following methods:

  • Loops.for(init, test, incr, action, [callback])
  • Loops.while(test, action, [callback])
  • Loops.do(action, while, [callback])

Loops.for

Increments a variable until a test condition evaluates to a falsey, passing the current iteration of the variable to an action function.

Syntax

Loops.for(init, test, iter, action, [callback]);

Parameters

init An value or function, of the form function() {...}, to which the internal variable is initially set
test A sentinal value, or function of the form, function(value) {...}, which determines whether the loop should continue
incr The value by which the counter is increased at the end of each iteration
action A function, of the form function(value) {...}, which is performed for each iterated value
callback An optional function, of the form function(err, lastValue) {...} which is performed at the end of the iteration (or on first error)

Notes

  • If a value is supplied for init, this is the initial value for the internal loop variable
  • If a function of the form function() {...} is supplied for init, the function is evaluated and the internal loop value is initialized to its return value
  • If a function of the form function(currentValue) {...} is supplied for test, the function is evaluated for the current loop value; the loop continues if this returns a truthy result
  • If a value of the same type as init is supplied for test; the loop continues if the current loop value is less than the value supplied
  • For all other values supplied for test, the loop continues if the value is truthy; this can lead to infinite loops
  • If a value-type is supplied for incr, that value is added to the current loop value. No attempt is made to convert to a common type, it is purely an addition operation.
  • If a function of the form function(currentValue) {...} is supplied for incr, the next loop value is set to the return value of that function.

Examples

Classic JavaScript Loops.for
var arr = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
for (var i = 0, len = arr.length; i < len; i++) {
	console.log('the value of arr[%d] is %d', i, arr[i]);
}
console.log('done => last value of i was %d', i);
		
var arr = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
Loops.for(0, arr.length, 1, function(i) {
	console.log('the value of arr[%d] is %d', i, arr[i]);
}, function(err, i) {
	console.log('done; last value of i was %d', i);
});
		
var arr = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
for (var i = 0; i < arr3.length; i++) {
	console.log('the value of arr[%d] is %d', i, ar3[i]);
    arr.pop();
}
console.log('done; last value of i was %d', i);
		
var arr = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
Loops.for(0, function(i) { return i < arr.length; }, 1, function(i) {
	console.log('the value of arr[%d] is %d', i, arr[i]);
    arr.pop();
}, function(err, i) {
	console.log('done; last value of i was %d', i);
});
		

Loops.while

Repeatedly executes an action function while a test condition evaluates to a truthy value.

Syntax

Loops.while(test, action, [callback]);

Parameters

test A sentinal value, or function of the form, function(value) {...}, which determines whether the loop should continue
action A function, of the form function() {...} which is executed on each iteration
callback An optional function, of the form function(err) {...} which is performed at the end of the iteration (or on first error)

Notes

  • If a function of the form function() {...} is supplied for test, the function is evaluated; the loop continues if this returns a truthy result
  • For all other values supplied for test, the loop continues if the value is truthy; this can lead to infinite loops
  • As opposed to Loops.do, this will always perform the test before the action; if the test is never truthy, the action will not be performed

Examples

Classic JavaScript Loops.while
var date1 = (new Date()).getTime();
var date2 = date1 + 5;
while (date1 < date2) {
	console.log('waiting for %d, currently %d', date2, date1);
    date1 = (new Date()).getTime();
}
console.log('done');
		
var date1 = (new Date()).getTime();
var date2 = date1 + 5;
Loops.while(function() { return (date1 < date2); }, function() {
    console.log('waiting for %d, currently %d', date2, date1);
    date1 = (new Date()).getTime();
}, function(err) {
    console.log('done');
});
		

Loops.do

Executes an action function and then repeats it while a test condition evaluates to a truthy value.

Syntax

Loops.do(action, test, [callback]);

Parameters

action A function, of the form function() {...} which is executed on each iteration
test A sentinal value, or function of the form, function(value) {...}, which determines whether the loop should continue
callback An optional function, of the form function(err) {...} which is performed at the end of the iteration (or on first error)

Notes

  • If a function of the form function() {...} is supplied for test, the function is evaluated; the loop continues if this returns a truthy result
  • For all other values supplied for test, the loop continues if the value is truthy; this can lead to infinite loops
  • As opposed to Loops.while, this will always perform the action before the test; the action will be performed, at least once, even if the test is never truthy

Examples

Classic JavaScript Loops.do
var date1 = (new Date()).getTime();
var date2 = date1 + 5;
do {
	console.log('waiting for %d, currently %d', date2, date1);
    date1 = (new Date()).getTime();
}
while (date1 < date2);
console.log('done');
		
var date1 = (new Date()).getTime();
var date2 = date11 + 5;
Loops.do(function() {
    console.log('waiting for %d, currently %d', date2, date1);
    date1 = (new Date()).getTime();
}, function() { return (date1 < date2); }, function(err) {
    console.log('done');
});