是否有一个快捷方式代理决心/拒绝一个角度$ Q承诺延期? [英] Is there a shortcut to proxy-resolve/reject a promise to an Angular $q deferred?
问题描述
给定一个悬而未决的延迟( DFD
),以及当时能够承诺(诺
),这可能会或可能不会被推迟,是有办法代理的承诺进入延期?
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(值) - 解决了价值派生的承诺。如果该值是通过$ 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(承诺)
有效期/支持。另外,我无法使用 $ q.when
(这确实需要一个当时能),因为DEFERED的承诺已经回来了。
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.
该角版本1.2.x的。
The Angular version is 1.2.x.
推荐答案
是 deferred.resolve
需要一个承诺。
deferred.resolve(值)
调用解决与待处理的承诺的原因许诺等待传递的承诺,成为履行其履约价值或者其拒绝原因拒绝(或保持挂起永远,如果传递的承诺一样)。
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).
调用的决心与承诺,拒绝承诺的原因与传递的承诺拒绝的理由予以拒绝。
Calling resolve with a rejected promise causes promise to be rejected with the passed promise's rejection reason.
调用的决心与承诺兑现承诺的原因与传递的承诺的履约价值得以实现。
Calling resolve with a fulfilled promise causes promise to be fulfilled with the passed promise's fulfillment value.
调用决心与非承诺值,使得承诺与价值实现。
Calling resolve with a non-promise value causes promise to be fulfilled with that value.
从 Q参考
要回答你的问题的另一部分:
这就不清楚dfd.resolve(承诺)有效/支持。另外,我无法使用$ q.when(这确实需要一个当时能),因为DEFERED的承诺已经回来了。
""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
创建的承诺可以给一个以上的收件人。每个收件人可以致电。然后
这一承诺的方法。的承诺只能使用一次解决(或者为一个值或错误)。但容器可以通过一个以上的消费者来读取。
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.
一个承诺看作一个值,你可以得到的在未来的。然后
, .catch
和。最后
方法。您可以访问容器不止一次,但它的内容是不可变的,当它解决了。
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.
德$的 .success
和 .error
在<$ C方法p $ pcation $ C> $ HTTP 服务
Deprecation of the .success
and .error
methods in the $http
service
在AngularJS团队在他们的新发现的智慧已经决定取消precate的 .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.
(或者我应该说失败)有关德precation更多信息的 .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
方法和学习使用 。然后
, .catch
和。最后
从现在开始。
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的老版本可以开始迁移到。然后
, .catch
和。最后
方法。
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
});
请注意,。然后
方法返回的数据不同于 .success
方法。
Notice that the .then
method returns data differently than the .success
method.
还兼有消费者应该检查错误。
Also both consumers should check for errors.
传统AngularJS的所以即使用户就可以开始写 .success
和 .error
免费code
So even users of legacy AngularJS can start writing .success
and .error
free code.
这篇关于是否有一个快捷方式代理决心/拒绝一个角度$ Q承诺延期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!