question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using bluebird to promisify Node's http.request?

See original GitHub issue

Can anyone help me promisify Node’s http.request with bluebird?

Here is my try:

var http = require('http');
var Promise = require('./framework/libraries/bluebird');
var PromiseRequest = Promise.promisify(http.request);

var myRequest = PromiseRequest({
    method: 'GET',
    host: 'nodejs.org',
    port: 80,
    path: '/',
}).then(function(value) {
    console.log('value:', value);
});

console.log('myRequest:', myRequest);

Unfortunately this code never completes and the ‘then’ callback is never executed.

@petkaantonov - love the library! Especially how performant it is!

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
kirkouimetcommented, May 4, 2014

@benjamingr LOL, I literally was writing this when your response came in on my email:

// Resolve the promise when the response ends
response.on('end', function() {
    resolve(result);
});

Thank you!

1reaction
kirkouimetcommented, May 4, 2014

@benjamingr thanks! You sent me in the right direction. Can you take a second to review this code and let me know if I am doing anything weird or incorrectly?

var http = require('http');
var Promise = require('bluebird');

var PromiseRequest = Promise.method(function(options) {
    return new Promise(function(resolve, reject) { 
        var request = http.request(options, function(response) {
            // Bundle the result
            var result = {
                'httpVersion': response.httpVersion,
                'httpStatusCode': response.statusCode,
                'headers': response.headers,
                'body': '',
                'trailers': response.trailers,
            };

            // Build the body
            response.on('data', function(chunk) {
                result.body += chunk;
            });

            // Resolve the promise
            resolve(result);
        });

        // Handle errors
        request.on('error', function(error) {
            console.log('Problem with request:', error.message);
            reject(error);
        });

        // Must always call .end() even if there is no data being written to the request body
        request.end();
    });
});

var myRequest = PromiseRequest({
    method: 'GET',
    host: 'www.nodejs.org',
    port: 80,
    path: '/',
}).then(function(value) {
    console.log('value:', value);
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Promise.promisify
Returns a function that will wrap the given nodeFunction . Instead of taking a callback, the returned function will return a promise whose...
Read more >
node.js - NodeJS Promisify an existing API with BlueBird
Start off by promisifying a single function var MyModule = new(require('../lib/MyModule')); var Promise = require("bluebird"); var ...
Read more >
Bluebird NPM: Bluebird JS Promise with Example
Bluebird JS is a fully-featured Promise library for JavaScript. The strongest feature of Bluebird is that it allows you to “promisify” other ...
Read more >
Using Promises in Node.js Applications using Bluebird
The promise object accepts callbacks that get invoked when the operation either succeeds or, fails. Dependent asynchronous operations can be ...
Read more >
squarecapadmin/request-promise
The world-famous HTTP client "Request" now Promises/A+ compliant. Powered by Bluebird. Bluebird and Request are pretty awesome, but I found myself using the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found