callback vs async.js vs promise vs async / await

时间:2024-01-21 08:00:28

需求:

A、依次读取 A|B|C 三个文件,如果有失败,则立即终止。
B、同时读取 A|B|C 三个文件,如果有失败,则立即终止。

一、callback


需求A

  
let read = function (code) {
    if (code) {
        return true;
    } else {
        return false;
    }
}

let readFileA = function (callback) {
    if (read(1)) {
        return callback(null, "111");
    } else {
        return callback("a fail");
    }
}
let readFileB = function (callback) {
    if (read(1)) {
        return callback(null, "222");
    } else {
        return callback("b fail");
    }
}
let readFileC = function (callback) {
    if (read(1)) {
        return callback(null, "333");
    } else {
        return callback("c fail");
    }
}

readFileA(function (err, data) {
    if (err) {
        console.log("open file " + err);
        return;
    }
    console.log("读取 a.txt 成功!内容:" + data);
    readFileB(function (err, data) {
        if (err) {
            console.log("open file " + err);
            return;
        }
        console.log("读取 b.txt 成功!内容:" + data);
        readFileC(function (err, data) {
            if (err) {
                console.log("open file " + err);
                return;
            }
            console.log("读取 c.txt 成功!内容:" + data);
        });
    });
});

return:

读取 a.txt 成功!内容:111
读取 b.txt 成功!内容:222
读取 c.txt 成功!内容:333

需求B:太恶心了,不写了,总之很繁琐.

二、async.js


async.js库的详细介绍可以见:[待写]

需求A

async.series

var async = require("async");

let read = function (code) {
    if (code) {
        return true;
    } else {
        return false;
    }
}

let readFileA = function (callback) {
    if (read(1)) {
        return callback(null, "111");
    } else {
        return callback("a fail");
    }
}

let readFileB = function (callback) {
    if (read(0)) {
        return callback(null, "222");
    } else {
        return callback("b fail");
    }
}
let readFileC = function (callback) {
    if (read(1)) {
        return callback(null, "333");
    } else {
        return callback("c fail");
    }
}

async.series([readFileA, readFileB, readFileC],
    function (err, datas) {
        if (err) {
            console.log("open file " + err);
        }
        console.log(datas);
        return;
    });

当第二个readFileB()读取失败的话:

return:

open file b fail
[ '111', undefined ]

需求B

async.parallel

var async = require("async");

let read = function (code) {
    if (code) {
        return true;
    } else {
        return false;
    }
}

let readFileA = function (callback) {
    if (read(1)) {
        return callback(null, "111");
    } else {
        return callback("a fail");
    }
}

let readFileB = function (callback) {
    setTimeout(() => {
        if (read(0)) {
            return callback(null, "222");
        } else {
            return callback("b fail");
        }
    }, 1000);
}

let readFileC = function (callback) {
    if (read(1)) {
        return callback(null, "333");
    } else {
        return callback("c fail");
    }
}

async.parallel([readFileA, readFileB, readFileC],
    function (err, datas) {
        if (err) {
            console.log("open file " + err);
        }
        console.log(datas);
        return;
    });

当第二个readFileB()读取失败 (注意我给它加了setTimeout,为了体现跟上面串行结果的不一样) 的话:

return:

open file b fail
[ '111', undefined, '333' ]