In the codebase i picked up, we have such chains as
funcA(a,b){
funcB(a, funcC);
}
funcB(a,b,callback){
callback(a, funcD); // calls funcC
}
funcC(a,b,callback){
callback(a, funcE); // calls funcD
}
So the functions donT even know what they are calling as callback!..
Needless to say that it s really difficult to read follow this code..
Does it have to be this way? How can I improve this code?
Thanks!
asked Jan 8, 2015 at 15:18
Orkun
7,33810 gold badges64 silver badges106 bronze badges
-
1possible duplicate of Chaining functions with callbacks while keeping separation of concernslukaszfiszer– lukaszfiszer2015年01月08日 15:24:58 +00:00Commented Jan 8, 2015 at 15:24
-
busted :) but i think i ll keep this one.Orkun– Orkun2015年01月08日 15:50:19 +00:00Commented Jan 8, 2015 at 15:50
-
... as I think this is a more generic problem. And the answer to the other post might not solve this.Orkun– Orkun2015年01月08日 16:04:51 +00:00Commented Jan 8, 2015 at 16:04
1 Answer 1
Can the EventEmitter help you with your issue?
http://nodejs.org/api/events.html#events_emitter_on_event_listener
var emitter = require('events').EventEmitter;
function A(a,b) {
// hard work
emitter.emit('funcADone', a , b);
}
function B(a,b) {
var c = a + b;
emitter.emit('funcBDone', c);
}
function C(c) {
console.log(c);
}
emitter.on('funcADone', B);
emitter.on('funcBDone', C);
A(1,2);
Sign up to request clarification or add additional context in comments.
Comments
lang-js