Last active
January 9, 2019 12:09
-
-
Save thefasttracker/9d958ff3274df2f4d8f3bce2b045542f to your computer and use it in GitHub Desktop.
async/await
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 fetch = require ('node-fetch') | |
| /*-----async function declaration with error handle------*/ | |
| async function fetchGithubUser(handle) { | |
| const url = `https://api.github.com/users/${handle}` | |
| const response = await fetch(url) | |
| const body = await response.json() | |
| if (response.status !== 200) | |
| throw Error(body.message) | |
| return body | |
| } | |
| fetchGithubUser('thefasttracker') | |
| .then(user => { | |
| console.log(user.name) | |
| console.log(user.location) | |
| }) | |
| .catch(err => { | |
| cosole.log(`Error: ${err.message}`) | |
| }) | |
| *or try/catch | |
| async function showGithubUser(handle) { | |
| try { | |
| const user = awayt fetchGithubUser(handle) | |
| console.log(user.name) | |
| console.log(user.location) | |
| } catch(err) { | |
| console.error(`Error: ${err.message}`) | |
| } | |
| } | |
| showGithubUser('idonotexist') | |
| /*----------async function expression----------------*/ | |
| const showGithubUser = async (handle) => { | |
| const url = `https://api.github.com/users/${handle}` | |
| const response = await fetch(url) | |
| return await response.json() | |
| } | |
| (async () => { | |
| const user = await fetchGithubUser('thefasttracker') | |
| console.log(user.name) | |
| console.log(user.location) | |
| })() | |
| /*-------------------async class--------*/ | |
| class GithubApiClient { | |
| async fetchUser(handle) { | |
| const url = `https://api.github.com/users/${handle}` | |
| const response = await fetch(url) | |
| return await response.json() | |
| } | |
| } | |
| (async () => { | |
| const client = new GithubApiClient() | |
| const user = await client.fetchUser('thefasttracker') | |
| console.log(user.name) | |
| console.log(user.location) | |
| })() | |
| /*--------------multiple http requests concurrently--------*/ | |
| async function fetchFromGithub(endpoint) { | |
| const url = `https://api.github.com${endpoint}` | |
| const response = await fetch(url) | |
| return await response.json() | |
| } | |
| async function showUserAndRepos(handle) { | |
| const userPromise = fetchFromGithub(`/users/${handle}`) | |
| const reposPromise = fetchFromGithub(`/users/${handle}/repos`) | |
| const user = await userPromise | |
| const repos = await reposPromise | |
| console.log(user.name) | |
| console.log(`${repos.length} repos) | |
| } | |
| *or with Promise.all | |
| async function showUserAndRepos(handle) { | |
| const [user, repos] = await Promise.all([ | |
| fetchFromGithub(`/users/${handle}`) | |
| fetchFromGithub(`/users/${handle}/repos`) | |
| ]) | |
| console.log(user.name) | |
| console.log(`${repos.length} repos) | |
| } | |
| /*------------------ post http requests --------------*/ | |
| (async () => { | |
| const rawResponse = await fetch('https://httpbin.org/post', { | |
| method: 'POST', | |
| headers: { | |
| 'Accept': 'application/json', | |
| 'Content-Type': 'application/json' | |
| }, | |
| body: JSON.stringify({a: 1, b: 'Textual content'}) | |
| }); | |
| const content = await rawResponse.json(); | |
| console.log(content); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment