gpt4 book ai didi

jquery - 如果延迟已经解决,jquery 延迟回调是否同步?

转载 作者:行者123 更新时间:2023-12-01 00:47:55 26 4
gpt4 key购买 nike

我猜答案是肯定的,但代码并不是最容易破译的。并在 docs它说

Any callbacks added to the object with deferred.then(), deferred.always(), deferred.done(), or deferred.fail() are queued to be executed later. Calling deferred.resolve() or deferred.resolveWith() transitions the Deferred into the resolved state and immediately executes any doneCallbacks that are set.

但这并没有明确涵盖将回调附加到已解析的 Deferred 的情况。

最佳答案

如果 promise 已经解决/拒绝,则新的回调将同步执行。

来自jQuery.deferred() doc :

Once the object has entered the resolved or rejected state, it stays in that state. Callbacks can still be added to the resolved or rejected Deferred — they will execute immediately.

演示 (Fiddle):

var def = jQuery.Deferred(),
promise = def.promise();
console.log('before 1');
promise.done(function () {
console.log('done 1');
});
console.log('after 1');
def.resolve();
console.log('before 2');
promise.done(function () {
console.log('done 2');
});
console.log('after 2');

输出:

before 1
after 1
done 1
before 2
done 2
after 2

关于jquery - 如果延迟已经解决,jquery 延迟回调是否同步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22633492/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com