Reputation: 9761
I would like to use When-JS capability out of the promise returned by a jquery ajax call. Is there a conversion scheme ?
Edit 1
https://github.com/cujojs/when/blob/master/docs/api.md#api
Upvotes: 1
Views: 105
Reputation: 276296
Yes, Promises/A+ promises like When promises have this functionality by-design and they are built to assimilate jQuery thenables.
In order to convert any foreign thenable (like a jQuery promise) to a when promise, just wrap it in when
:
when($.get(...)).then(...
when(x)
- get a trusted promise forx
. Ifx
is a foreign thenable, a returns a promise that followsx
.
How thenables are assimilated is well specified in the Promises/A+ spec:
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
.
Upvotes: 3