2

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.

Alexey Katayev
  • 248
  • 2
  • 10
  • Please look in https://stackoverflow.com/questions/41063947/angular-1-6-0-possibly-unhandled-rejection-error – Santosh Shinde Jan 09 '17 at 04:47
  • http://stackoverflow.com/questions/41281515/possibly-unhandled-rejection-in-angular-1-6 – Santosh Shinde Jan 09 '17 at 04:47
  • https://github.com/angular/angular.js/blob/v1.6.1/CHANGELOG.md – Santosh Shinde Jan 09 '17 at 04:48
  • @SantoshShinde the question was not about why a different error is reported then the one thats was used to rejecting the promise, which is `undefined` in the given code, but why the OP gets the undhandled rejection warning when using `finally`. – t.niese Jan 09 '17 at 05:45

1 Answers1

3

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.

t.niese
  • 39,256
  • 9
  • 74
  • 101