javascriptpromisejquery-deferredes6-promisewhen-js

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


Solution

  • 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 thenable, 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.