简体   繁体   中英

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. Is there a conversion scheme ?

Edit 1

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.

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 for x . If x is a foreign thenable, a returns a promise that follows x .

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) . If x is a then able, it attempts to make promise adopt the state of x , under the assumption that x behaves at least somewhat like a promise. Otherwise, it fulfills promise with the value x .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM