How to do synchronous Promise.then() chaining in jQuery v3?
Hi,
we have a lot of code where we rely on the jQuery v2 behavior of synchronous calls for promise.then chaining:
- function getResolvedPromise() {
return $.Deferred().resolve("A");
}
console.log("code BEFORE processing stuff");
var prom = getResolvedPromise();
prom = prom.then(function (value) {
console.log("then1: " + value);
return "B";
});
prom = prom.then(function (value) {
console.log("then2: " + value);
return "C";
});
prom.done(function (value) {
console.log("done: " + value);
});
console.log("code AFTER processing stuff");
The expected output (processing order) is (and was with jQuery v2):
- "code BEFORE processing stuff"
"then1: A"
"then2: B"
"done: C"
"code AFTER processing stuff"
Because of the changes to the promises in jQuery v3 we now get:
- "code BEFORE processing stuff"
"code AFTER processing stuff"
"then1: A"
"then2: B"
"done: C"
I fully understand that this was done for Promises/A+ compatibility, and therefor a reasonable change.
But:This breaks a lot of our code. Is there any way to get the old behavior somehow? The upgrade guide suggests using done() instead of then(), but that doesn't work if you want to pass other values to the next then() in the chain.
Any suggestions how to achieve a compatible behavior?
Thanks in advance!
Malte.