Use eventual 'finally' to handle error flow

That way we do not mark eventual error as handled
This commit is contained in:
Mariusz Nowak
2016-07-07 12:17:51 +02:00
parent cd24707e87
commit 27f3a0d5e3

View File

@@ -29,17 +29,29 @@ require('../lib/registered-extensions').promise = function (ignore, conf) {
conf.emit('setasync', id, count);
};
var onFailure = function () {
if (!waiting[id]) return; // deleted from cache before resolved
if (!waiting[id]) return; // deleted from cache (or succeed in case of finally)
delete waiting[id];
delete promises[id];
conf.delete(id);
};
var hasFinally = (typeof promise.finally === 'function');
if (typeof promise.done === 'function') {
// Optimal promise resolution
promise.done(onSuccess, onFailure);
if (hasFinally) {
// Use 'finally' to not mark eventual error as handled
promise.done(onSuccess);
promise.finally(onFailure);
} else {
promise.done(onSuccess, onFailure);
}
} else {
// Be sure to escape error swallowing
promise.then(function () { nextTick(onSuccess); }, function () { nextTick(onFailure); });
if (hasFinally) {
promise.then(function () { nextTick(onSuccess); });
promise.finally(function () { nextTick(onFailure); });
} else {
promise.then(function () { nextTick(onSuccess); }, function () { nextTick(onFailure); });
}
}
});