Node js 异步执行流程控制模块Async compose

compose创建一个包括一组异步函数的函数集合,每个函数会消费上一次函数的返回值。把f(),g(),h()异步函数,组合成f(g(h()))的形式,通过callback得到返回值。

通过compose组合,f(g(h()))的形式,从内层到外层的执行的顺序。

var async = require('async');

function a(id,cb){
console.log("@@@@@@@@@function a:"+id);
cb(null,"function a"+new Date().getTime());
}


function b(id,cb){
console.log("@@@@@@@@@function b:"+id);
cb(null,"function b"+new Date().getTime());
}


function c(id,cb){
console.log("@@@@@@@@@function c:"+id);
cb(null,"function c cb:"+new Date().getTime());
}


var fu = async.compose(a,b,c);
fu(66,function(err,result){
console.log("====="+result);
});

result---------------------------------

@@@@@@@@@function c:4
@@@@@@@@@function b:function c cb:1404995404342
@@@@@@@@@function a:function b1404995404343
=====function a1404995404344

你可能感兴趣的:(node,js)