javascript - 有没有办法从 Jquery Deferred/Promise 到 When-JS Promise

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

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

编辑 1

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

最佳答案

是的,Promises/A+ promises 就像 When promises 具有这种设计的功能,它们是为了吸收 jQuery thenables 而构建的。

为了将任何外部 thenable(如 jQuery promise)转换为 when promise,只需将其包装在 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.

thenables 如何被同化the 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). 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.

关于javascript - 有没有办法从 Jquery Deferred/Promise 到 When-JS Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30219865/

相关文章:

javascript - 在 Node.js 的循环中等待 Promise,然后再进入下一次迭代

java - 在 grails 中创建并发线程/promise

javascript - 使用 Kris Kowal 的问题。如果在链式 promise 的整个生命周期中抛出任何错误,我应该如何捕捉?

javascript - Promise 未在 .each() 循环中解决

javascript - 在向链上传递 promise 的同时更改数据结果?

javascript - puppeteer 操作按钮按下

javascript - jQuery 函数循环遍历元素并根据与具有特定类名的元素相比的顺序设置类名

javascript - jQuery 延迟和 promise 无法正常工作

javascript - HTML5 本地存储 URL 前缀

javascript - 处理获取响应的正确方法是什么