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

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

问题描述

我担心我使用JQuery兼容的promises看到Parse的引用,因为我已经读过jQuery承诺允许消费者改变承诺的状态。是否可以使用已知符合 Promises / A + 的其他承诺实施(例如 ECMAScript 6实施,或 Bluebird )使用Parse JavaScript SDK?

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?

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

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。这个问题是最初询问的 Parse Developers group ,但未收到任何回复。

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

推荐答案


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

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承诺肯定不允许消费者改变他们的状态 1 (因为jQuery现在几年都没有这样做)。顺便说一句,他们也是A +兼容: - )

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:通过公共方法。所以不要超过大多数其他实现,即。


是否可以使用已知的另一个promise实现Promises / A +是否符合Parse JavaScript SDK?

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 ,这意味着您可以从然后返回您最喜欢的promise实现的回调Parse承诺并期望它能够正常工作完美无缺:

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(…)

您还可以使用它们将它们转换为实施的有效承诺 Promise.resolve ,例如:

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

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




通常我认为这是不可能的,但在v1中在Parse JavaScript SDK的.4.2中,Parse.Promise的实现将属性 _isPromisesAPlusCompliant 定义为 false 然后检查在图书馆的各种功能。

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.

他!虽然遗憾的是没有记录,但这个标志实际上允许你制作你的应用中的原生Parse.com承诺库A +:

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;

更新:在较新的版本中,这不会公开为一个强调的属性,但你必须调用(未记录的) Parse.Promise.enableAPlusCompliant()方法。有关详细信息,请参阅问题#57

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:


  • 中的例外情况回调被捕获并导致拒绝结果承诺,而不是全局错误。所以你可以在其中使用 throw

  • 如果你返回一个值从 onRejected 回调(第二个参数到然后),应该处理错误并完成结果承诺而不是被拒绝。

  • 所有然后回调是异步执行的。

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

这些确实完全解决了 jQuery固有的问题 Deferred 当前实施

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

我假设Parse正计划以静默方式迁移此 true 设置成为默认值,并正在测试是否会破坏用户的任何内容。我猜它即使没有文档也可以使用它。

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.


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

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:


  • 通过将它们与 Promise.resolve组合在一起来装饰API中的所有promise-returns方法,这基本上是@dancamper建议的

  • 覆盖 Parse.Promise ,并在你的库周围包装。

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

第二个似乎更有效和稳定,它更易于维护,因为当Parse更改其API时不需要调整。

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 解析为数组,而 Parse.Promise.when 使用多个参数解析(见下文)。你可能想要/需要保留它并使用 promise.when = oldPromise.when; 代替。

3:确保不要覆盖你的方法这里有自定义库。 Parse不需要这些方法,它们用于jQuery兼容性。

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.

请注意,Parse与jQuery一样,有时会使用多个值来解析它的promise,例如在 Parse._ajax 中。它在内部不依赖于此功能,但您应该检查您最喜欢的承诺库如何应对它们。

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中使用其他promise实现吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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