是否有 proxy-resolve/reject 对 Angular $q 延期承诺的捷径?
Is there a shortcut to proxy-resolve/reject a promise to an Angular $q deferred?
给定一个未解决的延期(dfd
)和一个可以延期的承诺(promise
),有没有办法'proxy'承诺成递延?
语义应该是这样的:
promise.then(dfd.resolve, dfd.reject);
The $q documentation 仅提及处理被拒绝的承诺(此外,仅提及以某种方式拒绝的承诺):
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)
是否为 valid/supported 变得不清楚。另外,我不能使用 $q.when
(它确实需要一个 then-able),因为 defered 的承诺已经返回。
Angular 版本为 1.2.x.
是的。
任何时候你将一个 promise 解析为另一个 promise(无论是通过 resolve()
还是从回调中返回),它都会隐式地等待另一个 promise。
事实上,不可能让一个 promise 实际解析到另一个 promise 实例(无需等待)。
是的,deferred.resolve
接受了承诺。
deferred.resolve(值)
调用带有未决承诺的 resolve 会导致承诺等待传递的承诺,以其实现值实现或以其拒绝原因被拒绝(或者永远保持未决状态,如果传递的承诺确实如此)。
使用被拒绝的承诺调用 resolve 会导致承诺被拒绝,并带有传递的承诺的拒绝原因。
使用已实现的承诺调用 resolve 会导致使用传递的承诺的实现值来实现承诺。
使用非承诺值调用 resolve 会导致使用该值实现承诺。
From the Q API Reference
回答你问题的另一部分:
""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
方法。 promise 只能被解决一次(要么是一个值,要么是一个错误)。但是容器可以被不止一个消费者读取。
将 promise 视为一个容器,您可以通过 .then
、.catch
和 [=16] 在未来 中获得 =] 方法。您可以多次访问该容器,但它的内容在解析时是不可变的。
弃用 $http
服务中的 .success
和 .error
方法
AngularJS 团队以他们新发现的智慧决定弃用 .success
和 .error
方法。那些方法有问题,我说 很好的摆脱。
有关 .success
和 .error
方法弃用(或者我应该说失败)的更多信息,请访问最新的 AngularJS $http Service API Docs.
我们应该避免.success
和.error
方法,从现在开始学会使用.then
、.catch
和.finally
OP 引用的 $q
服务参考已过时。如需最新版本,请访问 AngularJS $q Service API Docs.
更新旧版本 AngularJS v1.2
我做了一些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
方法。
两个消费者使用相同 $http
承诺的示例。
//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
方法 returns 数据与 .success
方法不同。
另外两个消费者都应该检查错误。
因此,即使是旧版 AngularJS 的用户也可以开始编写 .success
和 .error
免费代码。
给定一个未解决的延期(dfd
)和一个可以延期的承诺(promise
),有没有办法'proxy'承诺成递延?
语义应该是这样的:
promise.then(dfd.resolve, dfd.reject);
The $q documentation 仅提及处理被拒绝的承诺(此外,仅提及以某种方式拒绝的承诺):
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)
是否为 valid/supported 变得不清楚。另外,我不能使用 $q.when
(它确实需要一个 then-able),因为 defered 的承诺已经返回。
Angular 版本为 1.2.x.
是的。
任何时候你将一个 promise 解析为另一个 promise(无论是通过 resolve()
还是从回调中返回),它都会隐式地等待另一个 promise。
事实上,不可能让一个 promise 实际解析到另一个 promise 实例(无需等待)。
是的,deferred.resolve
接受了承诺。
deferred.resolve(值)
调用带有未决承诺的 resolve 会导致承诺等待传递的承诺,以其实现值实现或以其拒绝原因被拒绝(或者永远保持未决状态,如果传递的承诺确实如此)。
使用被拒绝的承诺调用 resolve 会导致承诺被拒绝,并带有传递的承诺的拒绝原因。
使用已实现的承诺调用 resolve 会导致使用传递的承诺的实现值来实现承诺。
使用非承诺值调用 resolve 会导致使用该值实现承诺。
From the Q API Reference
回答你问题的另一部分:
""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
方法。 promise 只能被解决一次(要么是一个值,要么是一个错误)。但是容器可以被不止一个消费者读取。
将 promise 视为一个容器,您可以通过 .then
、.catch
和 [=16] 在未来 中获得 =] 方法。您可以多次访问该容器,但它的内容在解析时是不可变的。
弃用 $http
服务中的 .success
和 .error
方法
AngularJS 团队以他们新发现的智慧决定弃用 .success
和 .error
方法。那些方法有问题,我说 很好的摆脱。
有关 .success
和 .error
方法弃用(或者我应该说失败)的更多信息,请访问最新的 AngularJS $http Service API Docs.
我们应该避免.success
和.error
方法,从现在开始学会使用.then
、.catch
和.finally
OP 引用的 $q
服务参考已过时。如需最新版本,请访问 AngularJS $q Service API Docs.
更新旧版本 AngularJS v1.2
我做了一些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
方法。
两个消费者使用相同 $http
承诺的示例。
//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
方法 returns 数据与 .success
方法不同。
另外两个消费者都应该检查错误。
因此,即使是旧版 AngularJS 的用户也可以开始编写 .success
和 .error
免费代码。