Why the inconsistency in async.js between waterfall and series / parallelLimit(1)?

StackOverflow https://stackoverflow.com/questions/23439838

  •  14-07-2023
  •  | 
  •  

質問

The following code works fine:

var fs         = require('fs');
var async      = require('async');
var addErrParm = function (err, done) {return function(exists) {
    done(err, exists);
}}
function testAsync() {var response = '';
    function checkIfTheFileExists(done) {
        fs.exists('file.txt', addErrParm(null, done));
    }
    function readTheFile(exists, done) {
        if (!exists) {
            done('notFound');
        } else {
            fs.readFile('file.txt', 'utf8', done);
        }
    }
    function showTheFile(err) {
        if (err) {
            response = err;
        } else {
            response = 'file was read';
        }
        console.log(response);
    }
    async.waterfall([ //  <-- waterfall
        checkIfTheFileExists,
        readTheFile
    ], showTheFile);
}
testAsync() // "file was read"

The following doesn't seem to work. The only difference is the first uses async.waterfall instead of async.series.

var fs         = require('fs');
var async      = require('async');
var addErrParm = function (err, done) {return function(exists) {
    done(err, exists);
}}
function testAsync() {var response = '';
    function checkIfTheFileExists(done) {
        fs.exists('file.txt', addErrParm(null, done));
    }
    function readTheFile(exists, done) {
        if (!exists) {
            done('notFound');
        } else {
            fs.readFile('file.txt', 'utf8', done);
        }
    }
    function showTheFile(err) {
        if (err) {
            response = err;
        } else {
            response = 'file was read';
        }
        console.log(response);
    }
    async.series([ //   <-- this is the only line of code that is different.
        checkIfTheFileExists,
        readTheFile
    ], showTheFile);
}
testAsync() //     <-- nothing was shown on the console, not even a blank line.

The async.series version did not log any response to the console. What is causing no response from the async.series version?

I also tried a version using async.parallelLimit setting the limit to 1. I expected it to run the tasks in series due to the limit but again received nothing on the console. Here is the async.parallelLimit version:

var fs         = require('fs');
var async      = require('async');
var addErrParm = function (err, done) {return function(exists) {
    done(err, exists);
}}
function testAsync() {var response = '';
    function checkIfTheFileExists(done) {
        fs.exists('file.txt', addErrParm(null, done));
    }
    function readTheFile(exists, done) {
        if (!exists) {
            done('notFound');
        } else {
            fs.readFile('file.txt', 'utf8', done);
        }
    }
    function showTheFile(err) {
        if (err) {
            response = err;
        } else {
            response = 'file was read';
        }
        console.log(response);
    }
    async.parallelLimit([  //    <--- this line is different
        checkIfTheFileExists,
        readTheFile
    ], 1, showTheFile); //   <--- and this line is different.  I added the "1".
}
testAsync() //           <--- produces nothing on the console.

The file does exist in all three test cases.

役に立ちましたか?

解決

The async.series version did not log any response to the console. What is causing no response from the async.series version?

You misunderstand the semantic of series. It's in order one after the other like waterfall but unlike waterfall each worker function is entirely independent. Thus the results of checkIfTheFileExists are NOT passed to readTheFile. Series doesn't do that.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top