0

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
3
  • 1
    possible duplicate of Chaining functions with callbacks while keeping separation of concerns Commented Jan 8, 2015 at 15:24
  • busted :) but i think i ll keep this one. Commented 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. Commented Jan 8, 2015 at 16:04

1 Answer 1

2

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);
answered Jan 8, 2015 at 16:25
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.