async-http-request

Simply wrapped request with Promise, so you can use async/await of ES7.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
async-http-request
001.0.46 years ago6 years agoMinified + gzip package size for async-http-request in KB

Readme

async-http-request
Simply wrapped request with Promise, so you can use async/await of ES7.

Installation

Using npm:
$ npm i -D async-http-request

Usage

Resolve with response data

"use strict";

const asyncRequest = require("async-http-request");
const url = "http://www.google.com";

(async function() {
    // Resolved with response data
    let body = await asyncRequest(url);
    console.log(body);
})();

Resolve with response object, instance of http.IncomingMessage

Note, await could only be used in async function. See async & await

"use strict";

const asyncRequest = require("async-http-request");
const url = "http://www.google.com";
const resolveWithResponse = true;

(async function() {
    // Resolved with response object
    let response = await asyncRequest({
        url,
        resolveWithResponse
    });
    console.log(response.statusCode);
    console.log(response.headers['content-type']);
})();

Resolve with raw object as same as requestr returned

With this object, you can use all methods that docr shows

"use strict";

const asyncRequest = require("async-http-request");
const request = require("request");
const fs = require("fs");

const url = "http://www.google.com";
const resolveWithRequest = true;

(async function() {
    // Resolved with response object
    let oReq = await asyncRequest({
        url,
        resolveWithRequest
    });
    console.log(oReq);

    // Sample 1, pipe stream
    oReq.pipe(fs.createWriteStream("foo.txt"));

    // Sample 2, events
    oReq
        .on("response", function(resp) {
            console.log(resp.statusCode);
        })
        .on("error", function(err) {
            console.log(err);
        });
})();

Error handling, resolve with Error

"use strict";

const asyncRequest = require("async-http-request");
const url = "http://www.google.com";

(async function() {
    // Something wrong like net work issue
    let err = await asyncRequest(url);
    console.log(err);
})();

Convenience methods of requestr

"use strict";

const asyncRequest = require("async-http-request");
const url = "http://www.google.com";

(async function() {
    // Something wrong like net work issue
    let body_get = await asyncRequest.get(url);
    console.log(body_get);

    let body_post = await asyncRequest.post({
        url,
        form: {
            name: "Foo"
        }
    });
    console.log(body_post);
})();