我可以在 Parse JavaScript SDK 中使用其他承诺实现吗? [英] Can I use other promise implementations in the Parse JavaScript SDK?

查看:19
本文介绍了我可以在 Parse JavaScript SDK 中使用其他承诺实现吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我担心我看到的使用 JQuery 兼容承诺解析的引用,因为我读过 jQuery 承诺 允许消费者改变承诺的状态.是否可以使用另一个已知的 Promises/A+ 兼容的 promise 实现(例如 ECMAScript 6 实现,或 Bluebird) 与 Parse JavaScript SDK?

通常我认为这是不可能的,但在 Parse JavaScript SDK 的 v1.4.2 中,Parse.Promise 的实现将属性_isPromisesAPlusCompliant"定义为 false,然后在库中的各种函数中检查该属性.

注意这个问题是 最初在 解析开发者组,但没有收到任何回复.

解决方案

我担心 Parse 使用与 jQuery 兼容的承诺,因为我已经读到 jQuery 承诺允许消费者改变承诺的状态.

你不必担心.与 jQuery 兼容"可能意味着很多事情,而且 Parse 承诺肯定不允许消费者改变他们的状态1(因为 jQuery 多年来也没有这样做).顺便说一句,它们也是 A+兼容"的 :-)

1:通过公共方法.所以不会超过大多数其他实现,即.

<块引用>

是否可以使用其他已知的 Promises/A+ 兼容 Parse JavaScript SDK 的 promise 实现?

是的.Parse SDK 确实会返回有效的 A+ thenables,这意味着您可以从您最喜欢的 promise 实现的 then 回调中返回 Parse 承诺,并期望它能够完美运行:

myCompliantPromise.then(function(res) {返回 parse_query.get(…);}).然后(…)

您还可以使用 Promise.resolve,例如:

Promise.resolve(parse_query.get(…)).then(…);

<块引用>

通常我会假设这是不可能的,但在 Parse JavaScript SDK 的 v1.4.2 中,Parse.Promise 的实现将属性 _isPromisesAPlusCompliant 定义为 false 然后在库中的各种函数中进行检查.

他!虽然不幸的是没有记录,但这个标志确实允许您制作原生 Parse.com 承诺库在您的应用中符合 A+ 标准:

Parse.Promise._isPromisesAPlusCompliant = true;

更新:在较新的版本中,这不是作为下划线属性公开的,而是您必须调用(未记录的)Parse.Promise.enableAPlusCompliant()方法.有关详细信息,请参阅问题 #57.

我已经查看了代码,这个标志基本上改变了 3 件事:

  • then 回调中的异常被捕获并导致拒绝结果承诺,而不是全局错误.所以你可以在它们中使用 throw.
  • 如果您从 onRejected 回调(then 的第二个参数)return 一个值,则应该处理错误并返回结果承诺被履行而不是被拒绝.
  • 所有then回调都是异步执行的.

这些确实解决了当前 jQuery Deferred 实现固有的问题时间.

我假设 Parse 计划静默迁移此 true 设置以成为默认设置,并正在测试它是否会破坏用户的任何内容.我猜想即使还没有记录,使用起来也很安全.

<块引用>

我想让所有 Parse API 都返回我的自定义库的承诺.

虽然可以做到,但没那么简单.基本上有两种方法:

  • 通过使用 Promise.resolve 组合它们来装饰 API 中所有返回 promise 的方法,这基本上是 @dancamper 建议的内容
  • 使用库的包装器覆盖 Parse.Promise.

第二个似乎更高效和稳定,它更易于维护,因为它不需要在 Parse 更改其 API 时进行调整.

Parse.Promise = (function(oldPromise, Promise) {函数承诺(){var res, rej;var p = 新承诺(函数(_res,_rej){res = _res;rej = _rej;});p.resolve = res;p.reject = rej;返回 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;//你可能不需要/想要这些³Promise.prototype.always = oldPromise.prototype.always;Promise.prototype.done = oldPromise.prototype.done;Promise.prototype.fail = oldPromise.prototype.fail;回报承诺;}(Parse.Promise, require("Bluebird")));//管他呢

2:Promise.all 解析为数组,而 Parse.Promise.when 解析为多个参数(见下文).您可能希望/需要保留它并使用 promise.when = oldPromise.when; 代替.
3:确保不要在此处覆盖自定义库的方法.Parse 不需要这些方法,它们是为了 jQuery 兼容性.

请注意,Parse 与 jQuery 一样,有时会使用多个值来解析其承诺,例如在 Parse._ajax 中.它在内部不依赖这个特性,但你应该检查你最喜欢的 Promise 库是如何处理它们的.

I am concerned about references I have seen to Parse using JQuery-compatible promises, as I have read that jQuery promises allow consumers to mutate the state of the promise. Is it possible to use another promise implementation that is known to be Promises/A+ compliant (e.g. the ECMAScript 6 implementation, or Bluebird) with the Parse JavaScript SDK?

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" as false which is then checked in various functions within the library.

N.B. This question was originally asked on the Parse Developers group, but received no responses.

解决方案

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.

You don't need to be concerned. "jQuery-compatible" can mean a lot of things, and Parse promises do certainly not allow consumers to mutate their state1 (as jQuery doesn't do either since years now). Btw, they're A+ "compatible" as well :-)

1: through the public methods. So not more than most other implementations, that is.

Is it possible to use another promise implementation that is known to be Promises/A+ compliant with the Parse JavaScript SDK?

Yes. The Parse SDK does return valid A+ thenables, which means that you can return Parse promises from then callbacks of your favourite promise implementation and expect it to work flawlessly:

myCompliantPromise.then(function(res) {
    return parse_query.get(…);
}).then(…)

You can also cast them into valid promises of your implementation by using Promise.resolve, for example:

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 as false which is then checked in various functions within the library.

He! Although it is unfortunately undocumented, this flag does actually allow you to make the native Parse.com promise library A+ compliant in your app:

Parse.Promise._isPromisesAPlusCompliant = true;

Update: In newer versions, this is not exposed as an underscored property, but rather you have to call the (undocumented) Parse.Promise.enableAPlusCompliant() method. For details see issue #57.

I've reviewed the code, and this flag basically changes 3 things:

  • Exceptions in then callbacks are caught and lead to the rejection of the result promise, instead of a global error. So you can use throw in them.
  • If you return a value from the onRejected callback (second parameter to then), the error is supposed to be handled and the result promise is fulfilled instead of being rejected.
  • All then callbacks are executed asynchronously.

These are indeed solving exactly the problems inherent to the jQuery Deferred implementation at the current time.

I'll assume that Parse are planning to silently migrate this true setting to become the default, and are testing whether it breaks anything for the users. I'd guess that it is pretty safe to use even if undocumented yet.

I'd like to make all Parse APIs return promises of my custom library.

That's not so simple, although it can be done. There are basically two approaches:

  • decorate all promise-returning methods in the API by composing them with Promise.resolve, which is basically what @dancamper suggested
  • overwriting Parse.Promise with a wrapper around your library.

The second seems to be more efficient and stable, it's more maintainable as it doesn't require tweaking when Parse change their 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 resolves to an array, while Parse.Promise.when resolves with multiple arguments (see below). You may want / need to preserve this and use promise.when = oldPromise.when; instead.
3: Make sure not to overwrite methods of your custom library here. Parse doesn't need these methods, they're for jQuery compatibility.

Notice that Parse does, like jQuery, sometimes resolve its promises with multiple values, e.g. in Parse._ajax. It doesn't rely on this feature internally, but you should check how your favourite promise library copes with them.

这篇关于我可以在 Parse JavaScript SDK 中使用其他承诺实现吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆