A lightweight flow control for Node and Browsers. Similar to async.js but more than enjoyable API :).
npm install mid.js --save
Didn't use any flow control:
setTimeout(function(){
var a = doSomething()
setTimeout(function(){
doSomething2(a)
setTimeout(function(){
console.log('done')
}, 1000)
}, 1000)
}, 1000)
use mid.js
var mid = require('mid.js')
mid('sync')
.use(function(next){
setTimeout(function(){
var a = doSomething()
next(a)
}, 1000)
})
.use(function(a, done){
setTimeout(function(){
doSomething(a)
done()
}, 1000)
})
.done(function(){
setTimeout(function(){
console.log('done')
}, 1000)
})
async
var mid = require('mid.js')
mid('async')
.use(function(){
$.ajax({})
})
.use(function(){
$.ajax({})
})
.once(function(){
// here will trigger when all async process done
})
in loop:
var mid = require('../mid')
var masync = mid('async')
for(var i = 0; i < 2; i++){
masync.use(function(done){
setTimeout(function (){
done()
}, 1000)
})
}
masync.once(function (){
console.log('done')
})
set run use
functions as async
or sync
process functions for async
or sync
.
The behavior like Express.js middleware.
invoking when ecah processes done.
invoking once when all async process done.