@Ispanec1998

Почему finally в промисах не обрабатывает ошибку?

Прочитал, что finally это аналог then( () => {}, () => {} )
Визуально тут обработка ошибки тоже, но если у нас результат ошибка, то почему finally не обрабатывает ее, а промис виснит?
fetch().finally( () => 'error')
  • Вопрос задан
  • 102 просмотра
Решения вопроса 1
Rsa97
@Rsa97
Для правильного вопроса надо знать половину ответа
If the handler throws an error or returns a rejected promise, the promise returned by finally() will be rejected with that value instead. Otherwise, the return value of the handler does not affect the state of the original promise.
...
Unlike Promise.resolve(2).then(() => 77, () => {}) (which will return a resolved promise with the result 77), Promise.resolve(2).finally(() => 77) will return a new resolved promise with the result 2.
Similarly, unlike Promise.reject(3).then(() => {}, () => 88) (which will return a resolved promise with the value 88), Promise.reject(3).finally(() => 88) will return a rejected promise with the reason 3.
But, both Promise.reject(3).finally(() => {throw 99}) and
Promise.reject(3).finally(() => Promise.reject(99))
will reject the returned promise with the reason 99.

https://developer.mozilla.org/en-US/docs/Web/JavaS...
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы