是否有代理解析/拒绝对 Angular $q 延迟的承诺的快捷方式? [英] Is there a shortcut to proxy-resolve/reject a promise to an Angular $q deferred?

查看:25
本文介绍了是否有代理解析/拒绝对 Angular $q 延迟的承诺的快捷方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个未解决的延迟 (dfd) 和一个可以延迟的承诺 (promise),它可能会或可能不会被推迟,有没有办法代理' 承诺进入延期?

Given an unresolved deferred (dfd), and a then-able promise (promise), which may or may not be deferred, is there a way to 'proxy' the promise into the deferred?

语义应该是这样的:

promise.then(dfd.resolve, dfd.reject);

$q 文档只提到处理被拒绝的承诺(此外,只有以某种方式拒绝的承诺):

The $q documentation only mentions handling of rejected promises (and furthermore, only promises rejected in a certain way):

defered.resolve(value) – 用值解析派生的 promise.如果值是通过 $q.reject 构造的拒绝,则承诺将被拒绝.

defered.resolve(value) – resolves the derived promise with the value. If the value is a rejection constructed via $q.reject, the promise will be rejected instead.

这使得不清楚 dfd.resolve(promise) 是否有效/支持.另外,我不能使用 $q.when (它确实需要 then-able),因为延迟的承诺已经返回.

This makes it unclear if dfd.resolve(promise) is valid/supported. Also, I cannot use $q.when (which does take a then-able) because the promise of the defered has already been returned.

Angular 版本是 1.2.x.

The Angular version is 1.2.x.

推荐答案

是的,deferred.resolve 接受承诺.

deferred.resolve(value)

使用挂起的 Promise 调用 resolve 会导致 Promise 等待已传递的 Promise,以其完成值实现或被拒绝原因拒绝(或永远保持挂起状态,如果传递的 Promise 确实如此).

Calling resolve with a pending promise causes promise to wait on the passed promise, becoming fulfilled with its fulfillment value or rejected with its rejection reason (or staying pending forever, if the passed promise does).

使用被拒绝的 promise 调用 resolve 会导致 promise 被传递的 promise 的拒绝原因拒绝.

Calling resolve with a rejected promise causes promise to be rejected with the passed promise's rejection reason.

使用已履行的承诺调用 resolve 会导致承诺以传递的承诺的履行价值得到履行.

Calling resolve with a fulfilled promise causes promise to be fulfilled with the passed promise's fulfillment value.

使用非承诺值调用 resolve 会导致使用该值实现承诺.

Calling resolve with a non-promise value causes promise to be fulfilled with that value.

来自 Q API 参考

回答问题的另一部分:

""这使得不清楚 dfd.resolve(promise) 是否有效/支持.另外,我不能使用 $q.when (它确实需要 then-able),因为延迟的承诺已经返回.""

""This makes it unclear if dfd.resolve(promise) is valid/supported. Also, I cannot use $q.when (which does take a then-able) because the promise of the defered has already been returned.""

deferred.promise 创建的承诺可以提供给多个收件人.每个接收者都可以对该承诺调用 .then 方法.承诺只能解决一次(要么是值,要么是错误).但是容器可以被多个消费者读取.

The promise created by deferred.promise can be given to more that one recipient. Each recipient can call the .then method on that promise. The promise can only be resolved once (either to a value or an error). But the container can be read by more that one consumer.

将承诺视为一个值的容器,您可以在未来通过 .then.catch.finally 方法.您可以多次访问该容器,但其内容在解析时是不可变的.

Think of a promise as a container for a value which you can get in the future by the .then, .catch, and .finally methods. You can visit the container more than once but its contents are immutable when it resolves.

弃用 $http 服务中的 .success.error 方法

Deprecation of the .success and .error methods in the $http service

AngularJS 团队以其新发现的智慧决定弃用 .success.error 方法.这些方法有问题,我说摆脱困境.

The AngularJS team in their new found wisdom have decided to deprecate the .success and .error methods. Those methods were buggy and I say good riddance.

有关 .success.error 方法的弃用(或者我应该说失败)的更多信息,请访问最新的 AngularJS $http 服务 API 文档.

For more information on the deprecation (or should I say failure) of the .success and .error methods visit the latest AngularJS $http Service API Docs.

我们应该避免使用.success.error方法,学会使用.then.catch> 和 .finally 从现在开始.

We should avoid the .success and .error methods and learn to use the .then, .catch, and .finally from now on.

OP 引用的 $q 服务参考已过时.如需最新版本,请访问 AngularJS $q 服务 API 文档.

The $q service reference cited by the OP is dated. For the latest version, visit AngularJS $q Service API Docs.

旧版 AngularJS v1.2 的更新

我做了一些洞穴探险在 AngularJS Github 中.遗留的 $http 服务创建了一个 $q 承诺 (L750) 并随后附上有问题的 .success 方法 (L769) 和有问题的 .error 方法 (L776).

I did some spelunking in the AngularJS Github. The legacy $http service creates a $q promise (L750) and subsequently attaches the buggy .success method (L769) and the buggy .error method (L776).

这意味着那些坚持使用旧版 AngularJS 的人可以开始迁移到 .then.catch.finally方法.

What this means is that people stuck with using the older version of AngularJS can start migrating to the .then, .catch, and .finally methods.

具有相同 $http 承诺的两个消费者的示例.

An example with two consumers of the same $http promise.

//Producer
var httpPromise = $http.get(url);

//Consumer #1
httpPromise.then (function (response) {
                vm1.data = response.data;
        }) .catch (function (err) {
                //check for error
        });

//Consumer #2
httpPromise.then (function (response) {
                vm2.data = response.data;
        }) .catch (function (err) {
                //check for error
        });

请注意,.then 方法返回的数据与 .success 方法不同.

Notice that the .then method returns data differently than the .success method.

另外两个消费者都应该检查错误.

Also both consumers should check for errors.

因此,即使是旧版 AngularJS 的用户也可以开始编写 .success.error 免费代码.

So even users of legacy AngularJS can start writing .success and .error free code.

这篇关于是否有代理解析/拒绝对 Angular $q 延迟的承诺的快捷方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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