Using async await along with fetch

Trying to use asyc await with fetch:

I am trying to learn and use the new async await feature of Javascript but so far i only get a pending Promise message. I would like to know how i could resolve it

Code below


async function asyncData(){
  const response = await fetch('https://swapi.co/api/people/')
  const data = await response.json()
  return data;
}
const result = asyncData();
console.log(result);

async functions always return a promise.

You have to resolve it one way or another:

async function asyncData(){
  const response = await fetch('https://swapi.co/api/people/')
  const data = await response.json()
  return data;
}

(async () => {
  const result = await asyncData();
  console.log(result);
})()
async function asyncData() {
	const response = await fetch('https://swapi.co/api/people/');
	const data = await response.json();
	return data;
}

const result = asyncData();

result.then(data => console.log(data));
2 Likes