I use a third-party library. Used by node domain for error handling. If the callback function passed to this third-party library has any error, it ultimately calls the callback several times.
Code example:
var startFunction = function (callback) { //Call thirdParty function and wait for response thirdPartyFunction(function (error, data) { console.log("Called with"); console.log(arguments); //Assume there is an error in my callback function setTimeout(function () { dd callback.apply(null); }, 2000); }); } //ThirdParty function don't modify anything here var thirdPartyFunction = function (callback) { var Domain = require("domain"); var d = require('domain').create(); d.on('error', function (er) { console.log("hi"); callback.apply(null, er); }); d.run(function () { setTimeout(function () { callback.apply(null, [null, "Hello"]); }, 1000); }); }; startFunction(function () { console.log("Got response") });
We reported this error to a third-party library, and they changed the source code. For instance:
d.on('error', function (er) { if (isCalled == false) { isCalled = true; } else { return; } console.log("hi"); callback.apply(null, er); });
Now the problem of the function called repeatedly is solved. But the final callback will never be called.
How to handle this node behavior?
If the third-party version of lib changes the code to, this will lead to a crash of the application. Putting a wrapper domain also doesn't help.
d.on('error', function (er) { if (isCalled == false) { isCalled = true; } else { throw new Error("Getting called"); return; } console.log("hi"); callback.apply(null, er); });
What is the best way to pass such cases to node?
source share