[英]How do I resolve an expected Ember.PromiseProxyMixin rejection?
I ran into this odd case when I was attempting to use the PromiseProxyMixin
to clean up my templates. 当我尝试使用
PromiseProxyMixin
清理模板时遇到了这种奇怪的情况。 One of the situations was when I had a component that rendered a list after a search is completed with our API. 一种情况是,当我有一个用我们的API完成搜索后呈现列表的组件。 The searching was debounced because it triggers when the search term changes.
搜索被删除,因为它会在搜索字词更改时触发。 I then wrote a cancelation mechanism that would reject the last pending promise before making a new one
this.set('promise', newPromise)
. 然后,我编写了一个取消机制,该机制将在做出新的
this.set('promise', newPromise)
之前拒绝最后一个未决的承诺。 I started noticing that these rejected promises were being spit out to our logging service because the Ember.onerror
were trapping all unhandled rejections even though the Component I had expected that. 我开始注意到这些拒绝的承诺正在向我们的日志记录服务吐出,因为
Ember.onerror
捕获了所有未处理的拒绝,即使我曾期望过该组件。
Normally in a promise chain you can avoid unhandled errors by making sure you place a .catch()
at the end of the chain. 通常,在
.catch()
链中,可以通过确保在链的末尾放置.catch()
来避免未处理的错误。 However, I don't know how to do this if the promise is meant to be used in a PromiseProxyMixin
. 但是,如果
PromiseProxyMixin
使用了promise,我不知道该怎么做。
Here is a JSBin that illustrates the problem with expected rejections in a PromiseProxyMixin
. 这是一个JSBin,它说明了
PromiseProxyMixin
预期的拒绝问题 。
Well as it turns out at the time of assigning the promise you can use this.catch
to prevent the underlying promise to be unhandled and still set the isRejected
and reason
correctly: 事实证明,在分配承诺时,您可以使用
this.catch
防止未处理基础承诺,并且仍然正确设置isRejected
和reason
:
this.set('promise', newPromise);
this.catch(function(err) {
// Check if we should handle this or blow up
});
Here is an example from the previous code that illustrates this. 这是先前代码中的一个示例 ,说明了这一点。
An example might look like: 一个例子可能看起来像:
actions: {
goSearch: function() {
this.set('promise', cancellablePromise(this.get('query')));
this.catch(function(err) {
if (! err instanceof CancellationError) {
throw err; // Unexpected rejection; should be considered unhandled.
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.