The following chain is to be executed sequentially
$.when()
.pipe(functionA)
.pipe(functionB)
.fail(functionC);
where functionA
returns a Promise.
function functionA() {
return networkCall().then(sideEffectsForSuccess).catch(sideEffectsForFailure);
}
function networkCall() {
return new Promise(function (resolve, reject) {
return $.ajax(...);
});
}
However, this doesn't seem to work, since functionB
gets immediately executed without waiting for the Promise returned from functionA to resolve (or fail). The chain works correctly if functionA
is changed to
function functionA() {
return $.ajax(...).pipe(sideEffectsForSuccess, sideEffectsForFailure);
}
Unfortunately, I have to use networkCall
. Any ideas how to make this work?
CodePudding user response:
Can you return the promise
function functionA() {
let callPromise = networkCall();
callPromise.then(sideEffectsForSuccess).catch(sideEffectsForFailure);
return callPromise;
}
CodePudding user response:
Your implementation of functionA
is wrong.
function networkCall() {
return new Promise(function (resolve, reject) {
return $.ajax(...);
});
}
This returns a promise alright, but it does not return the promise it should return.
function networkCall() {
return $.ajax(...);
}
$.ajax()
already gives you a promise. Return that.