0.0.5 • Published 9 years ago

mid.js v0.0.5

Weekly downloads
1
License
MIT
Repository
github
Last release
9 years ago

mid.js

A lightweight flow control for Node and Browsers. Similar to async.js but more than enjoyable API :).

Installation

npm install mid.js --save

Examples

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')
})

API

mid('async'|'sync')

set run use functions as async or sync

use(function(...args, next))

process functions for async or sync. The behavior like Express.js middleware.

done(function)

invoking when ecah processes done.

once(function)

invoking once when all async process done.

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago