为什么瀑布和series / parallelLimit(1)之间的async.js不一致?

下面的代码工作正常:

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" 

以下似乎没有工作。 唯一的区别是第一次使用async.waterfall而不是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. 

async.series版本没有logging对控制台的任何响应。 什么导致async.series版本没有响应?

我也尝试了使用async.parallelLimit将限制设置为1的版本。我期望它由于限制而运行串行任务,但是在控制台上再次没有收到任何东西。 这里是async.parallelLimit版本:

 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. 

该文件确实存在于所有三个testing用例中。

async.series版本没有logging对控制台的任何响应。 什么导致async.series版本没有响应?

你误解了系列的语义。 它像瀑布一样依次而不像瀑布每个工人的function是完全独立的。 因此checkIfTheFileExists的结果不会传递给readTheFile 。 系列没有这样做。