为什么承诺会这样解决? (使用通知回调)
Why is the promise resolved like this? (Using notify callback)
我正在使用 angular 检查 Stomp Chat 客户端的代码,开发人员创建了一个名为 receive 的服务,其中 returns 是一个承诺。然后在控制器中,它解决了承诺但是......我不明白这个结构。
ChatService.receive().then(null, null, function(message) {
$scope.messages.push(message);
});
据我了解,then有两个参数,onSuccess函数和onError函数。那为什么前两个是空的,第三个是处理成功的?如果我需要处理错误,我该怎么做?
第三个参数是 notifyCallBack
函数。
来自文档:
The Promise API
Methods
then(successCallback, [errorCallback], [notifyCallback])
– regardless of when the promise was or will be resolved or rejected, then calls one of the success or error callbacks asynchronously as soon as the result is available. The callbacks are called with a single argument: the result or rejection reason. Additionally, the notify callback may be called zero or more times to provide a progress indication, before the promise is resolved or rejected.
This method returns a new promise which is resolved or rejected via the return value of the successCallback
, errorCallback
(unless that value is a promise, in which case it is resolved with the value which is resolved in that promise using promise chaining). It also notifies via the return value of the notifyCallback
method. The promise cannot be resolved or rejected from the notifyCallback
method. The errorCallback
and notifyCallback
arguments are optional.
注意:
Promise 通知没有很好地组合1 and are not part of ES6 Promises. Instead, consider using rxJS Observables which are used extensively in Angular 2+,为未来提供了更好的路径。
我正在使用 angular 检查 Stomp Chat 客户端的代码,开发人员创建了一个名为 receive 的服务,其中 returns 是一个承诺。然后在控制器中,它解决了承诺但是......我不明白这个结构。
ChatService.receive().then(null, null, function(message) {
$scope.messages.push(message);
});
据我了解,then有两个参数,onSuccess函数和onError函数。那为什么前两个是空的,第三个是处理成功的?如果我需要处理错误,我该怎么做?
第三个参数是 notifyCallBack
函数。
来自文档:
The Promise API
Methods
then(successCallback, [errorCallback], [notifyCallback])
– regardless of when the promise was or will be resolved or rejected, then calls one of the success or error callbacks asynchronously as soon as the result is available. The callbacks are called with a single argument: the result or rejection reason. Additionally, the notify callback may be called zero or more times to provide a progress indication, before the promise is resolved or rejected.This method returns a new promise which is resolved or rejected via the return value of the
successCallback
,errorCallback
(unless that value is a promise, in which case it is resolved with the value which is resolved in that promise using promise chaining). It also notifies via the return value of thenotifyCallback
method. The promise cannot be resolved or rejected from thenotifyCallback
method. TheerrorCallback
andnotifyCallback
arguments are optional.
注意:
Promise 通知没有很好地组合1 and are not part of ES6 Promises. Instead, consider using rxJS Observables which are used extensively in Angular 2+,为未来提供了更好的路径。