ES2017 async/await with fetch in Redux (No Callback Hell anymore)
When we need to use third party API or any REST API in our web app, we need to wait for the response coming from the server.
Then in the success callback , we use that data and do other operations.And if we have more REST calls, we all know that it ...
hashnode.com
This is great, and I use this in my own async action creators. One thing that should be added is checking for a 404 or 500 response, because
fetch()won't throw an error if the request succeeded, regardless of the response.So the lines making the fetch call and parsing the data would look like this:
const response = await fetch(`${API_SERVER}/speakers`); if (!response.ok) { throw new Error(response.statusText || 'Invalid response'); } const speakers = await response.json();See this site for a longer explanation