<分区>
对于 jQuery 的 promise ,不带参数的 $.when()
返回已解决的 promise 。返回被拒绝的 jQuery promise 的等价物是什么?
例如:
(function () { return $.when(); })()
.then(()=>console.log('resolved'))
.fail(()=>console.log('failed'));
打印“已解决”。我只想更改函数返回的内容,以便打印“失败”。
这是为了将同步内容与 jQuery promise 联系起来。我唯一想做的就是将整个东西包装在一个延迟对象中,例如我可能有的地方:
function doSomething () {
let success = ...;
return success ? $.when() : /* not sure */;
}
我可以这样做:
function doSomething () {
return $.Deferred(function (def) {
let success = ...;
if (success)
def.resolve();
else
def.reject();
}).promise();
}
但如果我不必像那样包装东西,而是可以直接返回类似 $.when()
的东西,那会更方便。