Reputation: 248
When I handle a promise with .finally() then I get this "Possibly unhandled rejection" error, so instead of .finally I have to use redundant .then() and .catch() with the same code inside. Is this a bug or there's a correct way to handle this rejection?
const deferred = $q.defer();
deferred.promise.finally(() => {});
deferred.reject();
Here's the fiddle.
Upvotes: 2
Views: 2383
Reputation: 40852
The finally
for promises has the same behaviour/intent as the finally
in try blocks.
If you write:
try {
throw new Error('test') // do some stuff
} finally {
console.log('finally')
}
Then the console.log('finally')
is executed, but after that the code will stop with a:
Uncaught Error: test
The same is with Promises, you can use finally
to execute code regardless if the promise is fulfilled or rejected, but you still need to use a catch
callback to handle the rejection.
Upvotes: 3