我想在 jquery ajax 调用返回的 promise 之外使用 When-JS 功能。有转换方案吗?
编辑 1
最佳答案
是的,Promises/A+ promises 就像 When promises 具有这种设计的功能,它们是为了吸收 jQuery thenables 而构建的。
为了将任何外部 thenable(如 jQuery promise)转换为 when promise,只需将其包装在 when
中即可。 :
when($.get(...)).then(...
when(x)
- get a trusted promise forx
. Ifx
is a foreign thenable, a returns a promise that followsx
.
thenables 如何被同化 在 the Promises/A+ 中有详细说明。规范:
The promise resolution procedure is an abstract operation taking as input a promise and a value, which we denote as
[[Resolve]](promise, x)
. Ifx
is athen
able, it attempts to make promise adopt the state ofx
, under the assumption thatx
behaves at least somewhat like a promise. Otherwise, it fulfills promise with the valuex
.
关于javascript - 有没有办法从 Jquery Deferred/Promise 到 When-JS Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30219865/