我有一个可以取消的蓝鸟承诺。取消后,我必须做一些工作来巧妙地中止正在运行的任务。可以通过两种方式取消任务:通过promise.cancel()
或promise.timeout(delay)
。
为了能够在取消或超时时巧妙地中止任务,我必须捕获CancellationErrors和TimeoutErrors。捕获CancellationError有效,但由于某种原因,我无法捕获TimeoutError:
var Promise = require('bluebird');
function task() {
return new Promise(function (resolve, reject) {
// ... a long running task ...
})
.cancellable()
.catch(Promise.CancellationError, function(error) {
// ... must neatly abort the task ...
console.log('Task cancelled', error);
})
.catch(Promise.TimeoutError, function(error) {
// ... must neatly abort the task ...
console.log('Task timed out', error);
});
}
var promise = task();
//promise.cancel(); // this works fine, CancellationError is caught
promise.timeout(1000); // PROBLEM: this TimeoutError isn't caught!
如何在设置超时之前捕获超时错误?
答案 0 :(得分:5)
当您取消承诺时,只要找到仍然可以取消的父母,取消就会首先向其父母发泡,这与仅传播给儿童的正常拒绝非常不同。
.timeout
做了一个简单的正常拒绝,它没有取消,所以这就是为什么不可能这样做。
您可以在延迟后取消:
var promise = task();
Promise.delay(1000).then(function() { promise.cancel(); });
或在任务函数中设置超时:
var promise = task(1000);
function task(timeout) {
return new Promise(function (resolve, reject) {
// ... a long running task ...
})
.timeout(timeout)
.cancellable()
.catch(Promise.CancellationError, function(error) {
// ... must neatly abort the task ...
console.log('Task cancelled', error);
})
.catch(Promise.TimeoutError, function(error) {
// ... must neatly abort the task ...
console.log('Task timed out', error);
});
}
您还可以创建一个方法:
Promise.prototype.cancelAfter = function(ms) {
var self = this;
setTimeout(function() {
self.cancel();
}, ms);
return this;
};
然后
function task() {
return new Promise(function (resolve, reject) {
// ... a long running task ...
})
.cancellable()
.catch(Promise.CancellationError, function(error) {
// ... must neatly abort the task ...
console.log('Task cancelled', error);
})
}
var promise = task();
// Since it's a cancellation, it will propagate upwards so you can
// clean up in the task function
promise.cancelAfter(1000);