1.3.5 • Published 7 years ago
conti v1.3.5
conti
A primitive JavaScript asynchronous function manager.
Install
npm install conti
Examples
var conti = require("conti");
sequential execution
conti.exec([
function(done){
setTimeout(done, 1000);
},
function(done){
setTimeout(done, 1000);
}
], function(err){
if( err ){
throw new Exception(err);
}
console.log("done after 2 seconds");
});
parallel execution
conti.execPara([
function(done){
setTimeout(done, 1000);
},
function(done){
setTimeout(done, 1000);
}
], function(err){
if( err ){
throw new Exception(err);
}
console.log("done after 1 second");
});
sequential forEach
conti.forEach([1,2,3], function(ele, done){
setTimeout(done, ele*1000);
}, function(err){
if( err ){
throw new Exception(err);
}
console.log("done after 6 seconds");
});
parallel forEach
conti.forEachPara([1,2,3], function(ele, done){
setTimeout(done, ele*1000);
}, function(err){
if( err ){
throw new Exception(err);
}
console.log("done after 3 seconds");
});
parallel map
conti.mapPara([1,2,3], function(ele, cb){
setTimeout(function(){
cb(undefined, ele*10);
}, 1000);
}, function(err, result){
if( err ){
throw new Exception(err);
}
console.log(result, "after 1 second"); // result is [10, 20, 30]
})
dynamically add to a list of asynchronous calls that are invoked sequentially
I found this function to be useful while coding for single page applications.
conti.enqueue(function(done){
console.log("first start");
setTimeout(done, 1000);
}, function(err){ console.log("first end"); });
conti.enqueue(function(done){
console.log("second start");
setTimeout(done, 1000);
}, function(err){ console.log("second end"); });
// first start
// first end (after 1 second)
// second start
// second end (after another 1 second)
License
This software is released under the MIT License, see LICENSE.txt.