【问题标题】:Is there a way to go from Jquery Deferred/Promise to When-JS Promise有没有办法从 Jquery Deferred/Promise 到 When-JS Promise
【发布时间】:2015-07-25 00:55:19
【问题描述】:

我想在 jquery ajax 调用返回的承诺之外使用 When-JS 功能。有转换方案吗?

编辑 1

https://github.com/cujojs/when/blob/master/docs/api.md#api

【问题讨论】:

    标签: javascript promise jquery-deferred es6-promise when-js


    【解决方案1】:

    是的,Promises/A+ Promises 像 When Promise 具有这种功能设计,它们被构建为同化 jQuery thenables。

    为了将任何外部 thenable(如 jQuery 承诺)转换为 when 承诺,只需将其包装在 when

    when($.get(...)).then(...
    

    when(x) - 获得x 的可信承诺。如果x 是一个外来的thenable,a 返回一个跟在x 之后的promise。

    thenables 如何被同化the Promises/A+ 规范中有详细说明:

    promise 解析过程是一个抽象操作,将 promise 和 value 作为输入,我们将其表示为[[Resolve]](promise, x)。如果xthenable,它会尝试让promise 采用x 的状态,假设x 的行为至少有点像promise。否则,它会以 x 的值履行承诺。

    【讨论】:

      猜你喜欢
      • 2017-06-21
      • 1970-01-01
      • 2012-11-03
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多