"Callback was already called" error firing inapropriately
See original GitHub issueI 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:
- Created 8 years ago
- Comments:7
And 5 minutes later I found what was wrong on our end… Example if it helps
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.
@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?