await fetch in react 1

await fetch in react

async function fetchFunction() {
  try{
	const response = await fetch(`http://url.com`);
	const json = await response.json();
  }
  catch(err) {
    throw err;
    console.log(err);
  }
}

Here is what the above code is Doing:
1. We’re using the async keyword to declare an asynchronous function.
2. We’re using the await keyword to wait for the response from the fetch call.
3. We’re using the await keyword again to wait for the json() method’s promise to resolve.
4. We’re using the try…catch statement to catch any errors that may occur in the try block.

Similar Posts