Last active
January 9, 2019 12:09
-
-
Save thefasttracker/9d958ff3274df2f4d8f3bce2b045542f to your computer and use it in GitHub Desktop.
Revisions
-
thefasttracker revised this gist
Jan 9, 2019 . 1 changed file with 16 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -94,3 +94,19 @@ async function showUserAndRepos(handle) { 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); })(); -
thefasttracker revised this gist
May 5, 2017 . 1 changed file with 87 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,96 @@ 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) } -
thefasttracker revised this gist
May 5, 2017 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -6,4 +6,6 @@ async function showGithubUser(handle) { const user = await response.json() console.log(user.name) console.log(user.location) } showGithubUser('thefasttracker') -
thefasttracker revised this gist
May 5, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ const fetch = require ('node-fetch') async function showGithubUser(handle) { const url = `https://api.github.com/users/${handle}` const response = await fetch(url) const user = await response.json() console.log(user.name) -
thefasttracker created this gist
May 5, 2017 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,9 @@ const fetch = require ('node-fetch') async function showGithubUser(handle) { const url = 'https://api.github.com/users/${`handle`} const response = await fetch(url) const user = await response.json() console.log(user.name) console.log(user.location) }