Skip to content

Instantly share code, notes, and snippets.

@thefasttracker
Last active January 9, 2019 12:09
Show Gist options
  • Select an option

  • Save thefasttracker/9d958ff3274df2f4d8f3bce2b045542f to your computer and use it in GitHub Desktop.

Select an option

Save thefasttracker/9d958ff3274df2f4d8f3bce2b045542f to your computer and use it in GitHub Desktop.

Revisions

  1. thefasttracker revised this gist Jan 9, 2019. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions gistfile1.txt
    Original 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);
    })();
  2. thefasttracker revised this gist May 5, 2017. 1 changed file with 87 additions and 2 deletions.
    89 changes: 87 additions & 2 deletions gistfile1.txt
    Original 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)
    const user = await response.json()
    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)
    }

    showGithubUser('thefasttracker')
    *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)
    }
  3. thefasttracker revised this gist May 5, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion gistfile1.txt
    Original 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')
  4. thefasttracker revised this gist May 5, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original 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 url = `https://api.github.com/users/${handle}`
    const response = await fetch(url)
    const user = await response.json()
    console.log(user.name)
  5. thefasttracker created this gist May 5, 2017.
    9 changes: 9 additions & 0 deletions gistfile1.txt
    Original 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)
    }