node-async

install Async
 
bower install async --save
npm install async --save
jam install async
spm install async
component install caolan/async
 

.waterfall

async.waterfall([
  function(callback) {
    getUserById(userId, function(user) {
      callback(null, user);
    });
  },
  function(user, callback) {
    getDepartmentByUser(user.id, function(department) {
      callback(null, department);
    });
  },
  function(department, callback) {
    getUserById(department.id, function(users) {
      console.log(users)
      callback(null);
    });
  }
])

 

parallel

async.parallel([
  function(callback) {
    getUsers(function(users) {
      callback(null, users);
    });
  },
  function(callback) {
    getDepartments(function(departments) {
      callback(null, departments);
    });
  },
  function(callback) {
    getTasks(function(tasks) {
      callback(null, tasks);
    });
  }
], function(err, results) {
  console.log(results); // [users, departments, tasks]
});

series

async.series([
  function(callback) {
    if (...) {
      code1();
      callback(null);
    } else {
      code2();
      callback(null);
    }
  },
  function(callback) {
    getUsers();
  }
])

 

 

 

 
 
 

评论

此博客中的热门博文

XML, XSL, HTML

Input in element.eleme.io

Data URI是由RFC 2397 ACE