[英]Is there a way to go from Jquery Deferred/Promise to When-JS Promise
I would like to use When-JS capability out of the promise returned by a jquery ajax call. 我想在jquery ajax调用返回的promise中使用When-JS功能。 Is there a conversion scheme ? 有转换方案吗?
Edit 1 编辑1
https://github.com/cujojs/when/blob/master/docs/api.md#api https://github.com/cujojs/when/blob/master/docs/api.md#api
Yes, Promises/A+ promises like When promises have this functionality by-design and they are built to assimilate jQuery thenables. 是的,Promises / A +承诺如果承诺具有这种功能的设计,它们的构建是为了吸收jQuery thenables。
In order to convert any foreign thenable (like a jQuery promise) to a when promise, just wrap it in when
: 为了将任何外国thenable(像一个jQuery的承诺),当一个承诺,只是把它包装when
:
when($.get(...)).then(...
when(x)
- get a trusted promise forx
.when(x)
-获取可信的承诺,x
。 Ifx
is a foreign thenable, a returns a promise that followsx
. 如果x
是外来的,那么a返回一个跟随x
的promise。
How thenables are assimilated is well specified in the Promises/A+ spec: 在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)
. promise解析过程是一个抽象操作,它将promise和value作为输入,我们将其表示为[[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. 如果x
是then
能干,它试图使承诺采取的状态x
,即假设下x
的行为至少有点像一个承诺。 Otherwise, it fulfills promise with the valuex
. 否则,它满足x
值的承诺。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.