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.

"Callback was already called" error firing inapropriately

See original GitHub issue

I am still trying to isolate exactly how this is happening/carve out our erroring code so I can reasonably paste an example in here but I have some code that worked great before the multiple callback protection that now throws this error. Any tips on what might cause this (in my code or async) would be greatly appreciated. I have traced through our code and I can’t find any problems with our callbacks other than that we get this error.

To keep track of series being initialized and their final callback being called I I added the following to async.js:

  • a counter at the top of the file initialized to 0
  • add cod to async.series() to:
    • increment the counter
    • use that counter in console logs to see when a new series is initialized and when we hit the final callback for that series.

When I run my code I see: initializing series 1 initializing series 2 Series Callback 2 Series Callback 1 initializing series 3 initializing series 4 Series Callback 4 Series Callback 3 initializing series 5 [callback already called error]

Series 1 & 2 are nested inside a function and series 5 is the start of that same function being called again with different params. When we enter series 5 with these new parameters the first function just gets in an if statement then calls it’s callback and this is when the error fires.

    async.series(
                [
                    function(series_cb){
                        if (size === null || action === 'delete') {
                            //this next line causes the error
                            return series_cb();
                        }
                        ...

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
AndrewBarbacommented, Aug 23, 2015

And 5 minutes later I found what was wrong on our end… Example if it helps

function doSomething(list, next) {
  var result = ...
  _.each(list, function(item) {
    if (fails(item)) return next(new Error(...))
  });
  next(null, result)
}

It’s a pretty common pattern for us to check for an error and then return near the top of the function. In this case we did that in an underscore each which returns from the context of underscore but does not return from the enclosing function, and thus the callback was being called twice. Hope that helps someone else out there.

0reactions
samuelgoldenbaumcommented, May 2, 2017

@AndrewBarba could you perhaps add some more code showing the problem and fix? If did you deal with the guard clauses at the top of the function?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: Callback was already called · Issue #1954 - GitHub
An annoying timing problem due to being async. The client probably receives all the data and closes the connection. But we haven't processed...
Read more >
AsyncNodeJS: Error: Callback was already called
Error : Callback was already called. My code is: 'app.post('/api/updateGroup/:group_id', function (req, res) ...
Read more >
How to prevent disasters caused by the callback getting called ...
In this situation execute() is the function of the third party module which I am using, and this callback gets called multiple times...
Read more >
Service Portal Callback function isn't working - s... - ServiceNow
Solved: The goal is for a Service Portal's catalog item to trigger a lookup on the change_request table after a change number has...
Read more >
Changelog - Bluebird JS
Fix Promise.map and .map not always calling the callback asynchronously ... is not a function error ( #1192 ); Fix global events not...
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