我很担心我看到的使用与 JQuery 兼容的 promise 进行解析的引用,因为我读过 jQuery promise allow consumers to mutate the state of the promise .是否可以使用另一个已知为 Promises/A+ 的 promise 实现与 Parse JavaScript SDK 兼容(例如 ECMAScript 6 implementation 或 Bluebird )?
通常我认为这是不可能的,但在 Parse JavaScript SDK v1.4.2 中,Parse.Promise 的实现将属性“_isPromisesAPlusCompliant”定义为 false,然后在库中的各种函数中检查该属性。
注意这个问题是originally asked在 Parse Developers group 上,但没有收到任何回复。
最佳答案
I am concerned that Parse uses jQuery-compatible promises, as I have read that jQuery promises allow consumers to mutate the state of the promise.
您不必担心。 “jQuery 兼容”可能意味着很多事情,Parse promise 当然不允许消费者改变他们的状态1(因为 jQuery 多年来都没有这样做)。顺便说一句,它们也是 A+“兼容”的 :-)
1:通过公共(public)方法。所以不会超过大多数其他实现,也就是说。
Is it possible to use another promise implementation that is known to be Promises/A+ compliant with the Parse JavaScript SDK?
是的。 Parse SDK 会返回有效的 A+ thenables,这意味着您可以从 then
您最喜欢的 promise 实现的回调中返回 Parse promises 并期望它完美地工作:
myCompliantPromise.then(function(res) {
return parse_query.get(…);
}).then(…)
您还可以使用 Promise.resolve
将它们转换为您实现的有效 promise 。 ,例如:
Promise.resolve(parse_query.get(…)).then(…);
Normally I would assume that this isn’t possible, but in v1.4.2 of the Parse JavaScript SDK, the implementation of Parse.Promise defines the property
_isPromisesAPlusCompliant
asfalse
which is then checked in various functions within the library.
他!虽然不幸的是没有记录,但这个标志实际上允许您使在您的应用程序中兼容原生 Parse.com promise 库 A+:
Parse.Promise._isPromisesAPlusCompliant = true;
更新:在较新的版本中,这不会作为带下划线的属性公开,而是您必须调用(未记录的)Parse。 Promise.enableAPlusCompliant()
方法。详情见issue #57 .
我已经查看了 the code ,这个标志基本上改变了 3 件事:
then
回调中的异常被捕获并导致拒绝结果 promise ,而不是全局错误。所以你可以在其中使用throw
。- 如果您从
onRejected
回调(then
的第二个参数)返回
一个值,则应该处理该错误并返回结果 promise 得到履行而不是被拒绝。 - 所有
then
回调都是异步执行的。
这些确实解决了 problems inherent to the jQuery Deferred
implementation在当前时间。
我假设 Parse 计划静默迁移此 true
设置成为默认设置,并正在测试它是否会破坏用户的任何东西。我猜即使尚未记录,使用起来也相当安全。
I'd like to make all Parse APIs return promises of my custom library.
虽然可以做到,但并不那么简单。基本上有两种方法:
- 通过使用
Promise.resolve
组合 API 中的所有返回 promise 的方法来装饰它们,这基本上是@dancamper 的建议 - 用库的包装器覆盖
Parse.Promise
。
第二个似乎更高效和稳定,它更易于维护,因为它不需要在 Parse 更改其 API 时进行调整。
Parse.Promise = (function(oldPromise, Promise) {
function promise() {
var res, rej;
var p = new Promise(function(_res, _rej) {
res = _res;
rej = _rej;
});
p.resolve = res;
p.reject = rej;
return p;
}
promise.is = oldPromise.is;
promise.as = Promise.resolve;
promise.error = Promise.reject;
promise.when = Promise.all; // ²
promise._continueWhile = oldPromise._continueWhile;
Promise.prototype._continueWith = oldPromise.prototype._continueWith;
Promise.prototype._thenRunCallback = oldPromise.prototype._thenRunCallback;
// you might not need / want these ³
Promise.prototype.always = oldPromise.prototype.always;
Promise.prototype.done = oldPromise.prototype.done;
Promise.prototype.fail = oldPromise.prototype.fail;
return promise;
}(Parse.Promise, require("Bluebird"))); // or whatever
2:Promise.all
解析为数组,而 Parse.Promise.when
解析为多个参数(见下文)。您可能想要/需要保留它并改用 promise.when = oldPromise.when;
。
3:确保不要在此处覆盖自定义库的方法。 Parse 不需要这些方法,它们是为了 jQuery 兼容性。
请注意,Parse 确实像 jQuery 一样,有时会使用多个值来解决它的 promise ,例如在 Parse._ajax
中。它在内部不依赖此功能,但您应该检查您最喜欢的 promise 库如何处理它们。
关于javascript - 我可以在 Parse JavaScript SDK 中使用其他 promise 实现吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30896859/