> For the complete documentation index, see [llms.txt](https://strctr.gitbook.io/programming/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://strctr.gitbook.io/programming/01-languages/javascript/01-language/e-controls/e4-promises/04.ii-promise-catch.md).

# 04.ii Promise.catch()

Метод `promise.catch(onRejected)` возвращает новый Promise реагирующий только на переход в rejected состояние. Если `catch` обработчик отработал, то Promise считается возвращенным в fulfilled состояние.

```javascript
const promise = Promise.resolve("Success.");
promise
  .then(value => {
    console.log(value); // "Success!"
    return Promise.reject("Failure.");
  })
  .catch(e => {
    console.log(e); // Failure
  })
  .then(
    () => {
      console.log("after a catch the chain is restored");
    },
    () => {
      console.log("Not fired due to the catch");
    }
  );
// Success.
// Failure.
// after a catch the chain is restored
```

Errors thrown inside asynchronous functions will act like uncaught errors:

```javascript
const promise = new Promise((resolve, reject) => {
  setTimeout(() => throw "Uncaught Exception!", 1000);
});

promise.catch(e => {
  console.log(e); // This is never called
});
```

Errors thrown after resolve is called will be silenced:

```javascript
const promise = new Promise((resolve, reject) => {
  resolve();
  throw "Silenced Exception!";
});

promise.catch(e => {
  console.log(e); // This is never called
});
```

## Catch errors on normal flow

Если есть Promise и на нем установлены две реакции на `fulfilled` и `rejected` случаи, то проблема в том, что исключение произошедшее в `fulfilled` случае не будет перехвачено в `rejected` обработчике:

```javascript
somethingAsync.then(
  () => {
    return somethingElseAsync();
  },
  err => {
    handleMyError(err);
  }
);
```

Решение:

```javascript
somethingAsync
  .then(() => {
    return somethingElseAsync();
  })
  .catch(err => {
    handleMyError(err);
  });
```

## Handling exceptions in Promise-based functions

If exceptions are thrown inside the callbacks of `then()` and `catch()` then that’s not a problem, because these two methods convert them to rejections.

However, things are different if you start your async function by doing something synchronous:

```javascript
function asyncFunc() {
    doSomethingSync(); // (A)
    return doSomethingAsync().then(result => {
        ···
    });
}
```

If an exception is thrown in line A then the whole function throws an exception. There are two solutions to this problem.

* returning a rejected Promise
* executing the sync code inside a callback

```javascript
//  Solution 1
function asyncFunc() {
    try {
        doSomethingSync();
        return doSomethingAsync()
            .then(result => {
                ···
            });
    } catch (err) {
        return Promise.reject(err);
    }
}

//  Solution 2
function asyncFunc() {
    return Promise.resolve()
    .then(() => {
        doSomethingSync();
        return doSomethingAsync();
    })
    .then(result => {
        ···
    });
}
```

## Unhandled rejected Promises

Before a rejection is reported, an event is dispatched that you can listen to:

```javascript
window.addEventListener('unhandledrejection', event => ···);
```

The event is an instance of `PromiseRejectionEvent` whose two most important properties are:

* `promise`: the Promise that was rejected
* `reason`: the value with which the Promise was rejected

The following example demonstrates how this event works:

```javascript
window.addEventListener("unhandledrejection", event => {
  // Prevent error output on the console:
  event.preventDefault();
  console.log("Reason: " + event.reason);
});

function foo() {
  Promise.reject("abc");
}
foo();
// Reason: abc
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://strctr.gitbook.io/programming/01-languages/javascript/01-language/e-controls/e4-promises/04.ii-promise-catch.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
