Skip to content

Instantly share code, notes, and snippets.

@Vterebenin
Created November 22, 2018 21:27
Show Gist options
  • Select an option

  • Save Vterebenin/699964718a797a7fa3c6a20dcf3cc2c4 to your computer and use it in GitHub Desktop.

Select an option

Save Vterebenin/699964718a797a7fa3c6a20dcf3cc2c4 to your computer and use it in GitHub Desktop.
fetchAPI via async
const getResource = async (url) => {
// await мы будем ждать пока результат не будет доступен.
const res = await fetch(url);
// если произошла ошибка сервера и один из статусов реально не доступен.
if (!res.ok) {
throw new Error(`Could not fetch ${url}` +
`, received ${res.status}`);
}
const body = await res.json();
return body;
};
getResource('https://swapi.co/api/people/1123123123')
.then((body) => {
console.log(body);
})// если произошла ошибка сети.
.catch((err) => {
console.error('Could not fetch', err);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment