Skip to content

Instantly share code, notes, and snippets.

@alexjpaz
Last active February 9, 2019 12:50
Show Gist options
  • Select an option

  • Save alexjpaz/ee3254140c7f15c873f077694286807d to your computer and use it in GitHub Desktop.

Select an option

Save alexjpaz/ee3254140c7f15c873f077694286807d to your computer and use it in GitHub Desktop.

Revisions

  1. alexjpaz revised this gist Feb 9, 2019. 1 changed file with 34 additions and 1 deletion.
    35 changes: 34 additions & 1 deletion request.js
    Original file line number Diff line number Diff line change
    @@ -1 +1,34 @@
    //
    // async /await
    const request = async (url) => {
    let client = require('http');

    if(url.startsWith('https')) {
    client = require('https');
    }

    await new Promise((res, rej) => {
    client.get(url, (rsp) => {
    rsp.on('end', () => {
    if(rsp.statusCode === 200) {
    res();
    } else {
    rej();
    }
    });
    })
    .on('error', rej);
    });
    };

    const main = async (url) => {
    try {
    await request(url);
    console.log('success');
    } catch(e) {
    console.error(e);
    process.exit(1);
    throw e;
    }
    };

    main("http://gooddgle.com");
  2. alexjpaz created this gist Feb 9, 2019.
    1 change: 1 addition & 0 deletions request.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    //