For jQuery's promises, $.when()
with no arguments returns a resolved promise. What is the equivalent that returns a rejected jQuery promise?
For example:
(function () { return $.when(); })()
.then(()=>console.log('resolved'))
.fail(()=>console.log('failed'));
Prints "resolved". I'd like to change only what the function returns so that it prints "failed".
This is for bridging synchronous stuff with jQuery promises. The only thing I've thought of to do is to wrap the whole thing in a deferred object, e.g. where I might have:
function doSomething () {
let success = ...;
return success ? $.when() : /* not sure */;
}
I could do this instead:
function doSomething () {
return $.Deferred(function (def) {
let success = ...;
if (success)
def.resolve();
else
def.reject();
}).promise();
}
But it would be more convenient if I didn't have to wrap things like that, and could return something like $.when()
directly instead.