Parameterization of callback variables for a chain of asynchronous functions in javascript, jQuery

I have a number for functions of the form:

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

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

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

Several times I want to implement these functions sequentially, for example:

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

But I also want the implementation option for one of them to be individually with a different callback, for example

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

Can someone suggest an elegant and versatile way to fix this. I use jQuery in this project to make fair play.

+4
source share
1 answer

$. Deferred () seems to do the trick through 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);
   }
);
-1
source

All Articles