3

How can I use Superagent but with the Promises A+ spec? I'm working on a project that uses both Superagent and Bluebird and I would like to use the .then() syntax but can't figure out an easy way to do this without writing my own wrapper code.

I see this project but don't want to have to use the .promise() on each call.

Are there any other existing modules which make it look more like Bluebird?

Something more like -

var request = ('superagent-wrapperModule');

request.get(url).then(..).catch(...) 

[EDIT] I've actually made a module to do this the way I'd prefer (similar to above example).

If anyone's interested - github link and npm link

Justin Maat
  • 1,965
  • 3
  • 23
  • 33
  • 1
    Without the using the `.promise()` at the end of each call how would the library know when you actually wanted to send the request? – idbehold Mar 09 '15 at 18:45
  • 2
    The thing is that `superagent` doesn't send the request until you call `end`, so even if you do something like [this](https://gist.github.com/victorkohl/caeb46566183dbe6715f), using `promisifyAll`, you'll have to call `endAsync` to send the request. – victorkt Mar 09 '15 at 18:47
  • @idbehold , I'm thinking you would need to actually create and return a new Promise explicitly (assuming using Bluebird here), and manually call call the resolve/rejects. I'll see if I can get a gist to explain it better. The other option is to probably rewrite Superagent to be Promise based. – Justin Maat Mar 09 '15 at 19:04
  • @victorkohl Ok, [here's a gist](https://gist.github.com/jxm262/b150fa87840f56fd8284) I made which demonstrates your idea, but adding the `.then( )` to the prototype chain. Seems to work perfectly, as far as I can tell. – Justin Maat Mar 16 '15 at 18:14
  • @JustinMaat Looks interesting, thanks for sharing. – victorkt Mar 16 '15 at 18:51

1 Answers1

3

Like @idbehold and @victorkohl commented, superagent requires a call to end to know that the request is being sent. To do so, the superagent-bluebird-promise adapter have chosen to use a .promise() method, which also takes an option object.

If you don't like that and don't need options, I would recommend that you simply define your own then method on the request objects:

var request = require('superagent-bluebird-promise');
request.Request.prototype.then = function(s, e) {
    return this.promise().then(s, e);
};

so that you can use

request.get(url).then(…).catch(…);

(I've also opened a Github issue on this)

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I like your method name much more than `.promise()` – idbehold Mar 09 '15 at 20:20
  • Well, it's not just "a method name" but the A+ interoperability feature :-) – Bergi Mar 09 '15 at 20:37
  • It might be a better idea to only add the `then()` method to the object after calling one of the get, post, put, patch, or delete methods on the superagent object. – idbehold Mar 09 '15 at 21:21
  • @idbehold: I have no idea how superagent works, don't all these functions on the exported object return `Request` instances? If `request` already is one, then what happens if you call `.end()` directly on it? – Bergi Mar 09 '15 at 21:24
  • @Bergi , I'll mark this as accepted. I'm adding the link to our ticket/discussion from github in case anyone else is interested. https://github.com/KyleAMathews/superagent-bluebird-promise/issues/15 – Justin Maat Mar 13 '15 at 16:04