New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for promises over callbacks #796
Comments
This can be done trivially by a library and published. One already exists https://npmjs.org/package/request-promise although I'm not sure if it supports the same API. The issue you're going to run in to though is that the return value from request is a stream. The callback option is for convenience but is often not even used in streaming mode. This is how request supports streaming and a callback option in the same API but you'd have to break streaming if you returned a promise instead. |
Got it, makes sense. Didn't think more about the complications to the stream implementation. |
@mikeal What about Personally I use |
A promise library can wrap request quite easily to provide this API. There is no syntactic advantage to adding this to request itself, it's actually more readable when wrapped normally by the promise library. |
Thx for your answer :) |
+1 for promises. It's really cumbersome to wrap. Particularly I like promises because it makes the error handling logic sooooo much simpler. |
@coolaj86 Check out https://github.com/leukhin/co-request for using generators instead of promises. Not saying generators are better than promises, but especially if you're interested in error handling, generators can be really helpful. With co-request you will be able to actually wrap your requests in try/catch statements and really throw your errors very easily. Additionally, co-request is just a wrapper that thunkifies requests for use with the co library and yield statements |
@netpoetica as soon as 0.12 lands I'm gonna be all over co like white on rice. |
The return value of The dominant compatibility pattern between modules is callbacks, you're going to need to learn to live with that. Both |
As a side note: It suffices to return a thenable (object with Here's the check in co: https://github.com/visionmedia/co/blob/master/index.js#L238 |
Note that bluebird also provides a way to wrap request: var Promise = require("bluebird");
Promise.promisifyAll(require("request"));
// Use request.getAsync(...) not request(..), it will not return a promise |
FYI, the latest release of Request-Promise is now 100% compatible with Request plus Promise support. Although you can even use piping, it is recommended to |
Now that ES6 Promises are getting more stable, I would expect a lovely library like this eventually would support promises over callbacks.
I don't know how the roadmap is looking, but I'd love promises to my requests :)
The text was updated successfully, but these errors were encountered: