Created
November 22, 2018 21:27
-
-
Save Vterebenin/699964718a797a7fa3c6a20dcf3cc2c4 to your computer and use it in GitHub Desktop.
fetchAPI via async
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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