Région de recherche :

Date :

https://stackoverflow.com › questions › 38235715

Fetch: reject promise and catch the error if status is not OK?

You could also reject the Promise from your status function as so: function status(res) { if (!res.ok) { return Promise.reject(res.statusText); } return res; } Or actually you could reject the promise with the message given by your endpoint.

https://stackoverflow.com › questions › 29473426

fetch: Reject promise with JSON error object - Stack Overflow

new Promise((resolve, reject) => { fetch(url) .then(async (response) => { const data = await response.json(); return { statusCode: response.status, body: data }; }) .then((response) => { if (response.statusCode >= 200 && response.statusCode < 300) { resolve(response.body); } else { reject(response.body); } }) });

https://developer.mozilla.org › en-US › docs › Web › API › Fetch_API › Using_Fetch

Using the Fetch API - Web APIs | MDN - MDN Web Docs

The fetch() function returns a Promise which is fulfilled with a Response object representing the server's response. You can then check the request status and extract the body of the response in various formats, including text and JSON, by calling the appropriate method on the response.

https://medium.com › ... › how-to-make-http-requests-using-fetch-api-and-promises-b0ca7370a444

How to make HTTP requests using Fetch API and Promises

You will learn how to make a HTTP request using Fetch API, learn the basics of a native JavaScript Promise object and how to chain Promises using the Promise.prototype.then() method.

How to make HTTP requests using Fetch API and Promises

https://plainenglish.io › blog › handling-unsuccessful-calls-of-fetch-api-7eef551b74c8

How to Handle Unsuccessful Fetch API Calls in JavaScript - Plain English

As you can see, the state of promise is rejected and hence a catch block can handle this error. You can print the response instead of returning it, but you'll get the same results. catch() block can handle syntax errors because if the syntax is wrong : fetch cannot succeed → so promise not returned→ so error goes to catch

https://javascript.info › promise-error-handling

Error handling with promises - The Modern JavaScript Tutorial

If we throw inside a .then handler, that means a rejected promise, so the control jumps to the nearest error handler. Here’s an example: new Promise((resolve, reject) => { resolve("ok"); }).then((result) => { throw new Error("Whoops!"); // rejects the promise }).catch(alert); // Error: Whoops!

https://developer.mozilla.org › fr › docs › Learn › JavaScript › Asynchronous › Promises

Comment utiliser les promesses - Apprendre le développement web - MDN

Avec une API fonctionnant avec des promesses, la fonction asynchrone démarre l'opération et renvoie un objet Promise. On peut alors attacher des gestionnaires à cette promesse et les gestionnaires seront exécutés lors du succès ou de l'échec de l'opération. Utiliser l'API fetch()

https://dev.to › thecharacterv › error-handling-in-the-javascript-fetch-api-1f7a

Error handling in the JavaScript Fetch API - DEV Community

The fetch API returns a promise which either resolves or rejects. If it resolves, it will go into "then", and if it rejects, it will go into "catch". When a status code other than 200 is received, that does not mean that fetch had a bad response. An example of this could be if the database failed to connect. The backend could be ...

Error handling in the JavaScript Fetch API - DEV Community

https://flaviocopes.com › javascript-promises-rejection

How to handle promise rejections - flaviocopes.com

Here’s an example using the Fetch API: fetch ('/data.json'). then (response => { console. log (response.status)}) What if there is an error during the fetch() call? Perhaps the network is unavailable. Or the network request returns an error. The promise will reject. A promise will look something like this: const thePromise = new ...

https://medium.com › @firatatalay › promises-and-the-fetch-api-3072859932d9

Promises and the Fetch API - Medium

On the other hand, a rejected promise means that there has been an error during the asynchronous task. And the example of fetching data from an API, an error would be for example, when the user...