How do I use bluebird promises with request-extensible?

270 views Asked by At

I want to use bluebird to write a promise-based asynchronous web client. Currently I am doing this with the request-promise package. At the top of my file I just put

    var Promise = require("bluebird");
    var request = require('request-promise');
    Promise.promisifyAll(request);

and I'm good to go.

Now I want to add HTTP caching. Without bluebird, the way I know to do this is to use request-extensible.

var requestExt = require('request-extensible');
var requestHttpCache = require('request-http-cache');
var httpRequestCache = new requestHttpCache({
  max: 1024 * 1024
});
var request = requestExt({
  extensions: [httpRequestCache.extension]
});

The request-extensible framework is asynchronous, but works via callback functions. I'd like to wrap it with bluebird so that I can use it with promises instead. How do I do that?

1

There are 1 answers

0
robertklep On

I don't request-extensible in a lot of detail, but it seems that the object it returns isn't necessarily compatible with "plain" request (it doesn't have any of the shortcut functions .get()/.post()/..., for instance).

So to wrap it by bluebird, it seems to me this is enough:

var Promise    = require('bluebird');
var requestExt = require('request-extensible');
var request    = Promise.promisify(requestExt({ ... }));

// Use like this:
request('URL').then(...).catch(...)

// Or this:
request({ OPTIONS }).then(...).catch(...)