码迷,mamicode.com
首页 > 其他好文 > 详细

async

时间:2016-04-11 15:47:04      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:

用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!‘);
        }
    });

 

async

标签:

原文地址:http://www.cnblogs.com/lswit/p/5378679.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!