Question

I have a number for functions of the form:

function A() {
    asychProcessA(
        someCallBack();
    )
}

function B() {
    asychProcessB(
        someCallBack();
    )
}

function C () {
    asychProcessC(
        someCallBack();
    )
}

Some times I will want to implement these functions in series like:

function A() {
    asychProcessA(
        functionB();
    )
}

But I also want the option of implementing one of these individually with some other callback like

function A() {
    asychProcessA(
        someOtherCallBack();
    )
}

Can some one suggest an elegant and versatile way to set this up. I am using jQuery in this project so that's fair game.

Was it helpful?

Solution

$.Deferred() seems to be doing the trick via the implementation below.

function A(input) {
    var def = $.Deferred();
    asychProcessA(
        someCallBack(); 
        def.resolve(someInput);
    )
    return def.promise();
}

function B(input) {
    var def = $.Deferred();
    asychProcessB(
        someCallBack(); 
        def.resolve(someInput);
    )
    return def.promise();
}

//Implementation1: Use B() as call back for A()

A(someInput1).done(
   function(){
      B(someInput2);
   }
);

//Implementation1: Use random() as call back for A()

A(someInput1).done(
   function(){
      random(someInput2);
   }
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top