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.

request should provide a custom util.promisified() version

See original GitHub issue

Code like

request('https://example.com', (error, response, body) => {
  ...
});

can’t be promisified through the usual util.promisify() function in Node 8.x because it goes beyond the standard (error, result) => … callback pattern. It should be pretty easy to fix that by providing a custom implementation that gives e.g. a Promise for a {response, body} object.

I know there’s request-promise, and I can’t really tell how much of request this affects, but it would be awesome if a library that is this popular had even just very simple support for it.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:19
  • Comments:6

github_iconTop GitHub Comments

1reaction
danielgindicommented, Dec 8, 2019

This is what should happen:

// npm install --save util.promisified
const boundUtilPromisified = require('util.promisified'); // Provides backwards compatibility

Object.define(request, boundUtilPromisified.custom, {
  enumerable: false,
  value: function () {
        return new Promise((resolve, reject) => {
            arguments[arguments.length] = (err, httpResponse, body) => {
                if (err) 
                    return reject(err);
                resolve({ httpResponse, body });
            };
            arguments.length++;
            request.apply(this, arguments);
        });
  }
});

Then you could use it like this:

let response = await util.promisify(request)('http://url/to/whatever');
// do whatever with response.httpResponse, and response.body

Or even:

let { httpResponse, body } = await util.promisify(request)('http://url/to/whatever');
// do whatever with `httpResponse` and `body`
0reactions
stale[bot]commented, Dec 13, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NodeJs: util.promisify where the callback function has multiple ...
You'll need to provide a custom promisify implementation for the function. const { promisify } = require('util') ...
Read more >
Node.js 8: `util.promisify()` - 2ality
Node.js 8 has a new utility function: util.promisify() . It converts a callback-based function to a Promise-based one.
Read more >
Promisification - The Modern JavaScript Tutorial
A call to promisify(f) returns a wrapper around f (*) . That wrapper returns a promise and forwards the call to the original...
Read more >
Returning Promises with Promisfy in Node.js - Bits and Pieces
In this tutorial, I'll introduce you to the Promisify utility available in the Util Package of Node.js. In a nutshell, Promisify is an...
Read more >
New util.promisify in Node.js - Bruno Scopelliti
I used to have too my personal utility to convert Node.js callback-based internals methods into returning-promise ones.
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