Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Superagent with A+ spec?

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

like image 882
Justin Maat Avatar asked Feb 19 '26 03:02

Justin Maat


1 Answers

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)

like image 66
Bergi Avatar answered Feb 21 '26 15:02

Bergi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!