async

用async比用promise爽多了

var async = require('async');
var fs = require('fs');

async.waterfall([
        function(callback) {
            fs.readFile('./1.txt', 'utf-8', function(err, data) {
                console.log('1: ', data);
                callback(null, data);
            });
        },
        function(arg, callback) {
            console.log('2: ', arg);
            fs.readFile('./2.txt', 'utf-8', function(err, data) {
                callback(null, arg + data, 'haha');
            });
        },
        function(arg, arg2, callback) {
            console.log('3: ', arg, arg2);
            fs.readFile('./3.txt', 'utf-8', function(err, data) {
                callback(null, arg + data);
            });
        }
    ],
    function(err, result) {
        if(err) {
            console.log('err: ' + err);
        } else if(result) {
            console.log('result: ' + result);
        } else {
            console.log('async error!');
        }
    });

 

posted @ 2016-04-11 15:37  流殇微  阅读(202)  评论(0编辑  收藏  举报