I am using different 3rd party modules in my project. I have seen, in some odd situations the 3rd party module which I am using is calling the callback multiple times.Is there any general approach which I can follow so avoid such situations.
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
I am using different 3rd party modules in my project. I have seen, in some odd situations the 3rd party module which I am using is calling the callback multiple times.Is there any general approach which I can follow so avoid such situations.
The answer depends on the reason why the callback called multiple times, possible are:- by design- when multiple parallel running functions callback
--
Have you looked at promises? Promises will be called only once, and they offer a pretty clean way of doing chains.
My current approach is to either prefer libs that expose promises or, when there is no such option, to wrap calls to the library in a promise.
I am using different 3rd party modules in my project. I have seen, in some odd situations the 3rd party module which I am using is calling the callback multiple times.Is there any general approach which I can follow so avoid such situations.
--
--
+1 for just using the `once` module and being done with it. Measure everything, and worry about the side effects when you see them. If you're already into a "huge project", this is (as always, in my opinion) the most pragmatic option.
try { doSomething(); } catch (err) { /* who cares? */ }
I am using different 3rd party modules in my project. I have seen, in some odd situations the 3rd party module which I am using is calling the callback multiple times.Is there any general approach which I can follow so avoid such situations.
--
--
> a connect is successfully fired and after that an error is being fired
I don't see why these multiple callbacks are a problem. They are happening exactly as they are supposed to if they are actually convenience callbacks for event emitters like http.createServer. You should handle each type of event appropriately.
On Tuesday, 8 October 2013 11:13:24 UTC+5:30, Mark Hahn wrote:> a connect is successfully fired and after that an error is being fired
I don't see why these multiple callbacks are a problem. They are happening exactly as they are supposed to if they are actually convenience callbacks for event emitters like http.createServer. You should handle each type of event appropriately.@Mark
Yes I agree to it. But think like its a module to execute a mysql query. So you are using a function execute() to execute a query. As an user why will you expect your callback to be called multiple times? If there is a error it should call it will an error or inform its successfully executed. It shouldn't say the query is successfully executed and later call the callback again with the error. Doesn't it make sense? Please correct me if I am wrong. I am new to Nodejs