3 ms·
> This is the problem with jQuery's current "promises": they only support scenario 1 above, omitting entirely support for scenarios 2–4. Actually, `pipe` kind-
by masklinn 14y ago
> This is the problem with jQuery's current "promises": they only support scenario 1 above, omitting entirely support for scenarios 2–4.
Actually, `pipe` kind-of handles case 3 as well: if a promise is returned from either `pipe` handler it's used as replacement for the `pipe`'s resolution (a value merely replaces the existing resolution or rejection value), so
promise.pipe(null, function () {
return $.when(42);
});
will replace a rejection of `promise` by a resolution to 42.
Also, there's a mistake with the text I believe:
> That means if you give a promise out to multiple consumers, they can interfere with its state.
No, if you give a deferred to consumers they can alter it but the result of `.promise()` doesn't provide mutator methods, only state change callbacks. Promise producers are supposed to return the result of `.promise()`, returning a non-rejected and non-resolved Deferred directly is an error. And while — as you note — Deferred#then doesn't behave per-spec, Deferred.pipe does create and return a new promise.
(jQuery's deferreds have other issues which your post hasn't touched, such as the very special handling of single-value resolutions.