promise.catch
时间: 2023-10-14 17:08:01 浏览: 103
promise.catch is a method in JavaScript that is used to handle errors or rejected promises. It is called after a promise is rejected and allows you to specify what actions to take in case of an error.
For example, if you have a promise that fetches data from an API and it fails, you can use the catch method to handle the error and display an error message to the user, instead of crashing the entire application.
Here's an example of using promise.catch:
```
fetch('https://some-api-url.com/data')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error fetching data:', error));
```
In the above example, if the fetch request fails, the catch method will log an error message to the console with the error that caused the failure.
阅读全文