jQuery's Deferred/promise has two outcomes: resolved and rejected. You can attach callbacks to the Deferred which are associated with either state. The conditions for attachment are done (associated with resolved), fail (associated with rejected), and always (associated with both states). I have unsuccessfully been trying to determine the sequence of the callbacks when the state moves from pending to non-pending; i.e., for done and always (or fail and always), what is the sequence in which the callbacks execute for each state of resolved and rejected?
jQuery的Deferred / promise有两个结果:已解决和被拒绝。您可以将回调附加到与任一状态关联的Deferred。附件的条件已完成(与已解决相关联),失败(与拒绝相关联)以及始终(与两个州相关联)。当状态从挂起移动到非挂起时,我没有成功地确定回调的顺序;即,对于完成和始终(或失败并且始终),回调对每个已解决和拒绝的状态执行的顺序是什么?
2 个解决方案
#1
11
@Malcolm's answer is indeed correct. The docs mention it in many places, including:
@Malcolm的回答确实是对的。文档在很多地方提到它,包括:
-
deferred.done()
anddeferred.fail()
– "Callbacks are executed in the order they were added." -
deferred.always()
– "When the Deferred is resolved or rejected, callbacks are executed in the order they were added" -
jQuery.ajax()
– "Promise callbacks —.done()
,.fail()
,.always()
, and.then()
— are invoked, in the order they are registered."
deferred.done()和deferred.fail() - “回调按照添加的顺序执行。”
deferred.always() - “当Deferred被解决或拒绝时,回调按照添加的顺序执行”
jQuery.ajax() - “Promise回调 - .done(),. fail(),. aways()和.then() - 按照它们的注册顺序被调用。”
Implementation details
Looking at the Deferred module, it uses the Callbacks module which implements a FIFO "callback list".
查看Deferred模块,它使用Callbacks模块实现FIFO“回调列表”。
Here's the call stack for adding callbacks to a Deferred object:
这是用于向Deferred对象添加回调的调用堆栈:
-
always()
-
done()
/fail()
-
Callbacks.add()
-
list.push()
– The callback function is pushed onto the end of the list.
list.push() - 回调函数被推送到列表的末尾。
-
Callbacks.add()list.push() - 将回调函数推送到列表的末尾。
-
done()/ fail()Callbacks.add()list.push() - 将回调函数推送到列表的末尾。
-
always()done()/ fail()Callbacks.add()list.push() - 回调函数被推送到列表的末尾。
And here's the call stack for resolving/rejecting the Deferred object:
这是用于解析/拒绝Deferred对象的调用堆栈:
-
resolve()
/reject()
-
resolveWith()
/rejectWith()
-
Callbacks.fireWith()
-
Callbacks.fire()
– The callbacks in the list are executed in FIFO order using afor
loop.
Callbacks.fire() - 列表中的回调使用for循环以FIFO顺序执行。
-
Callbacks.fireWith()Callbacks.fire() - 列表中的回调使用for循环以FIFO顺序执行。
-
resolveWith()/ rejectWith()Callbacks.fireWith()Callbacks.fire() - 列表中的回调使用for循环以FIFO顺序执行。
-
resolve()/ reject()resolveWith()/ rejectWith()Callbacks.fireWith()Callbacks.fire() - 列表中的回调使用for循环以FIFO顺序执行。
#2
4
Deferred objects process their callbacks in the order that the callback was added to the deferred object. There is no precedence to done()
over always()
, or vice versa. The behavior is identical whether you call resolved()
or reject()
.
延迟对象按照将回调添加到延迟对象的顺序处理它们的回调。对于always(),done()没有优先权,反之亦然。无论您调用resolve()还是reject(),行为都是相同的。
Please see the following jsfiddle example.
请参阅以下jsfiddle示例。
The comments on the original question are not correct, or at least not completely correct.
对原始问题的评论不正确,或者至少不完全正确。
#1
11
@Malcolm's answer is indeed correct. The docs mention it in many places, including:
@Malcolm的回答确实是对的。文档在很多地方提到它,包括:
-
deferred.done()
anddeferred.fail()
– "Callbacks are executed in the order they were added." -
deferred.always()
– "When the Deferred is resolved or rejected, callbacks are executed in the order they were added" -
jQuery.ajax()
– "Promise callbacks —.done()
,.fail()
,.always()
, and.then()
— are invoked, in the order they are registered."
deferred.done()和deferred.fail() - “回调按照添加的顺序执行。”
deferred.always() - “当Deferred被解决或拒绝时,回调按照添加的顺序执行”
jQuery.ajax() - “Promise回调 - .done(),. fail(),. aways()和.then() - 按照它们的注册顺序被调用。”
Implementation details
Looking at the Deferred module, it uses the Callbacks module which implements a FIFO "callback list".
查看Deferred模块,它使用Callbacks模块实现FIFO“回调列表”。
Here's the call stack for adding callbacks to a Deferred object:
这是用于向Deferred对象添加回调的调用堆栈:
-
always()
-
done()
/fail()
-
Callbacks.add()
-
list.push()
– The callback function is pushed onto the end of the list.
list.push() - 回调函数被推送到列表的末尾。
-
Callbacks.add()list.push() - 将回调函数推送到列表的末尾。
-
done()/ fail()Callbacks.add()list.push() - 将回调函数推送到列表的末尾。
-
always()done()/ fail()Callbacks.add()list.push() - 回调函数被推送到列表的末尾。
And here's the call stack for resolving/rejecting the Deferred object:
这是用于解析/拒绝Deferred对象的调用堆栈:
-
resolve()
/reject()
-
resolveWith()
/rejectWith()
-
Callbacks.fireWith()
-
Callbacks.fire()
– The callbacks in the list are executed in FIFO order using afor
loop.
Callbacks.fire() - 列表中的回调使用for循环以FIFO顺序执行。
-
Callbacks.fireWith()Callbacks.fire() - 列表中的回调使用for循环以FIFO顺序执行。
-
resolveWith()/ rejectWith()Callbacks.fireWith()Callbacks.fire() - 列表中的回调使用for循环以FIFO顺序执行。
-
resolve()/ reject()resolveWith()/ rejectWith()Callbacks.fireWith()Callbacks.fire() - 列表中的回调使用for循环以FIFO顺序执行。
#2
4
Deferred objects process their callbacks in the order that the callback was added to the deferred object. There is no precedence to done()
over always()
, or vice versa. The behavior is identical whether you call resolved()
or reject()
.
延迟对象按照将回调添加到延迟对象的顺序处理它们的回调。对于always(),done()没有优先权,反之亦然。无论您调用resolve()还是reject(),行为都是相同的。
Please see the following jsfiddle example.
请参阅以下jsfiddle示例。
The comments on the original question are not correct, or at least not completely correct.
对原始问题的评论不正确,或者至少不完全正确。