javascript fetch await code example

Example 1: async fetch api call

async function getUserAsync(name) {
  try{
    let response = await fetch(`https://api.github.com/users/${name}`);
    return await response.json();
  }catch(err){
    console.error(err);
    // Handle errors here
  }
}

Example 2: fetch await

async function getUserAsync(name) 
{
  let response = await fetch(`https://api.github.com/users/${name}`);
  let data = await response.json()
  return data;
}

getUserAsync('yourUsernameHere')
  .then(data => console.log(data));

Example 3: 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);
  }
}