Getting new error using Async with Node 6.2.2 "Callback already called"
See original GitHub issueWhat version of async are you using?
3.9.5
Which environment did the issue occur in (Node version/browser version)
Node 6.2.2 and 5.0.71.52
What did you do? Please include a minimal reproducable case illustrating issue.
This app
var fs = require('fs'),
async = require('async'),
_dir = './data/';
var writeStream = fs.createWriteStream('./log.txt',
{'flags' : 'a',
'encoding' : 'utf8',
'mode' : 0666});
async.waterfall([
function readDir(callback) {
fs.readdir(_dir, function(err, files) {
callback(err,files);
});
},
function loopFiles(files, callback) {
files.forEach(function (name) {
callback (null, name);
});
},
function checkFile(file, callback) {
fs.stat(_dir + file, function(err, stats) {
callback(err, stats, file);
});
},
function readData(stats, file, callback) {
if (stats.isFile())
fs.readFile(_dir + file, 'utf8', function(err, data){
callback(err,file,data);
});
},
function modify(file, text, callback) {
var adjdata=text.replace(/somecompany\.com/g,'burningbird.net');
callback(null, file, adjdata);
},
function writeData(file, text, callback) {
fs.writeFile(_dir + file, text, function(err) {
callback(err,file);
});
},
function logChange(file, callback) {
writeStream.write('changed ' + file + '\n', 'utf8',
function(err) {
callback(err);
});
}
], function (err) {
if (err) {
console.log(err.message);
} else {
console.log('modified files');
}
});
What did you expect to happen?
That I wouldn’t get an error
What was the actual result?
I’m getting
/home/examples/public_html/learnnode2-examples/chap3/node_modules/async/dist/async.js:837 if (fn === null) throw new Error(“Callback was already called.”); ^
Error: Callback was already called.
The callback function in the following section of the code
function loopFiles(files, callback) { files.forEach(function (name) { callback (null, name); }); },
Is lost on the second run.
I’ve run this code successfully through several versions of Node. I believe I did test this when Node 6 was first released.
In the second iteration, the function isn’t null, but something is happening in the Async code resulting in this error.
Issue Analytics
- State:
- Created 7 years ago
- Comments:12
Top GitHub Comments
I just ran slightly modified code in Ubuntu 16.04 in node 6.0.0 and 6.2.2. In both cases I got the same output:
Modified script:
Because you called callback many times in here:
Please use async.forEach for this case.