generic-iterator
v0.0.5
Published
A continuation passing style library to help the coding of asynced programs in Javascript/Node.js.
Downloads
1
Readme
generic-iterator
Example:
var Class = require('better-js-class');
var cps = require('cps');
var $U = require('underscore');
var Iterator = require('../lib/main.js');
var cb = function() {
var startTime = new Date();
return function(err, res) {
if(err) {
console.log('ERROR:', err);
if (err.stack) {
console.log(err.stack);
}
} else {
console.log('OK:', res);
}
console.log('time spent: ', new Date() - startTime);
}
}();
var Processor = Class({
process: function(entry, cb) {
console.log('start processing', entry);
setTimeout(function() {
console.log('finish processing', entry);
if (entry == 42) {
cb(new Error('stopped'));
} else {
cb();
}
}, 1000);
},
destroy: function() {
}
});
$U.extend(Processor, {
factory: function(cb) {
cb(null, new Processor());
}
});
var MyIterator = Class(Iterator, {
_init: function() {
this.parent._init.call(this, {
max: 10,
ProcessorClass: Processor
});
},
_traverse: function(proc, cb) {
cps.pfor(100, function(i, cb) {
console.log('outer loop: ', i);
proc(i, cb);
}, cb);
}
});
(function() {
var myIterator = new MyIterator();
myIterator.run(cb);
})();