-
Notifications
You must be signed in to change notification settings - Fork 145
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
wrapCallback doesn't work with single-argument-callbacks #334
Comments
Fixed in #247. |
Looking at the code, not sure it fixes the issue: var cb = function (err) {
if (err) {
push(err);
}
[...]
}; This will always push the value as an error in case of fs.exists and other callbacks with single argument. |
Oops. You're right. You mentioned |
Other libraries (Rx) deal with this problem by introducing wrapCallback and wrapNodeCallback functions. Maybe we could implement something like wrapCallback1? This use case appears quite often. Also we should probably remove the flatFilter example from the docs which requires exactly that behavior to work. |
I expected, that the mapper would allow me to map all arguments including the first. I don't see any reason why it shouldn't. |
It doesn't because Even if we add a non-error version, we can't call it Can you give other examples of this usecase (beyond Good point on the |
Not easy to cite the APIs off the top of my head :) The reason I ran into this problem in the first place was the HTTP core API. For example http.request and http.get methods. I assure you, there are tons of APIs in the wild with generic callbacks, especially on the clientside. I guess the way to use them would be: .flatMap(function(pkg) {
var requestOptions = { ... };
return _(function(push) {
http.get(requestOptions, function(response) {
push(null, response);
push(null, _.nil);
});
});
}); That's what I do now. |
Fair enough. I'm not too familiar with client-side libraries, so that may be why I don't see them very often. We may be able to support this in 3.0 by renaming
Yeah. That's how you'd do it. You can also define a custom function getStream(requestOptions) {
return _(function (push) {
http.get(requestOptions, function (response) {
push(null, response);
push(null, _.nil);
});
});
} |
I'm -1 on renaming @dypsilon: you could also wrap function cbToNode(fn) {
return function(...args) {
var cb = args.pop();
fn(...args, function(...cbArgs) {
cb(null, ...cbArgs);
});
}
}
var request = _.wrapCallback(cbToNode(http.request)); edit: released a quick npm module for this: @quarterto/cb-to-node |
Well, if we were going to support this, it'd be for generic non-node callbacks of arbitrary number of arguments, so "unary" wouldn't cut it. |
IMHO wrapGenericCallback sounds better. Also this introduces some API inconsistencies. The usual way to create a stream is _.(source) which is great, but very limited. Following the wrapCallback logic, there should rather be different methods for each source type:
Just a thought. |
Maybe it even makes sense to create a different library, something like highland-interop and extract all source methods into this one library. The constructor _.() would only support a generator function in this case. What do you think about this idea for 3.0? |
I'm not opposed to an interop library, but the things you mention aren't inconsistencies, so I'm not sure it's worth it. The |
By the way, since |
WrapCallback provides a callback function which expects two arguments:
This doesn't work with functions like fs.exists or http.get because they call the function only with the result. See the code in question.
This behavior breaks the example of flatFilter():
This code will always throw 'Uncaught, unspecified "error" event.'
The text was updated successfully, but these errors were encountered: