question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting new error using Async with Node 6.2.2 "Callback already called"

See original GitHub issue

What 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:closed
  • Created 7 years ago
  • Comments:12

github_iconTop GitHub Comments

1reaction
megawaccommented, Jun 27, 2016

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:

  • data1.txt, callback for the first item (data1.txt)
  • Error: callback was already called

Modified script:

var fs = require('fs'),
    async = require('async'),
    _dir = './data/';

async.waterfall([
   function readDir(callback) {
      fs.readdir(_dir, callback);
   },
   function loopFiles(files, callback) {
      files.forEach(function (name) {
         callback (null, name);
      });
   },
   console.log.bind(console)
], console.error)
$ ls data
> data1.txt  data2.txt
1reaction
ex1stcommented, Jun 27, 2016

Because you called callback many times in here:

function loopFiles(files, callback) {
    files.forEach(function(name) {
        callback(null, name);
    });
},

Please use async.forEach for this case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node: Callback was already called - Stack Overflow
You make a callback after you handle err in pool.query. The problem is that pool.query is an async task and node jumps over...
Read more >
Node.js v19.3.0 Documentation
Indicates the failure of an assertion. All errors thrown by the node:assert module will be instances of the AssertionError class. new assert.AssertionError( ...
Read more >
Home - Documentation
Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript. Although originally designed for use ...
Read more >
Mongoose v6.8.2: API docs
Used for declaring paths in your schema that should be 128-bit decimal floating points. Do not use this to create a new Decimal128...
Read more >
AWS Lambda function handler in Node.js
The third argument, callback , is a function that you can call in non-async handlers to send a response. The callback function takes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found