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.