-
Notifications
You must be signed in to change notification settings - Fork 147
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
Added toCallback method #493
Conversation
It should definitely not In general, I think the philosophy is that the user should be able to suppress all errors emitted by the library. Also, I don't think the current implementation quite handles all of the error cases. Specifically, if you have
or
the callback will receive The better way is probably to use a var value;
var hasValue = false; // In case an emitted value === null or === undefined.
stream.consume(function (err, x, push, next) {
if (err) {
push(null, _.nil); // Signal that the stream no longer cares about future values.
if (hasValue) {
// emit error.
}
else {
cb(err);
}
}
else if (x === _.nil) {
// Either cb() or cb(null, value);
}
else {
if (hasValue) {
push(null, _.nil); // Signal that the stream no longer cares about future values.
// emit error
}
else {
value = x;
hasValue = true;
}
}
}); |
I edited the tests quickly and found that this was the result for each case:
The second case will work since we're only pulling 2 values at most, and since Also yeah, that's a good point about being able to suppress errors, the only reason I shied away from doing that was because it would be hard to differentiate between errors coming from the stream vs errors coming from |
Ah, you're right.
Yeah, I understand your concern. But there's plenty of other ways for programmer errors to get to the errors of the callback, so I wouldn't worry too much much about separating them. For example, // This is a programmer error. You can't flat map to a number.
_([1]).flatMap(x => x)
.toCallback((err, x) => {
// err should say "Expected Stream: got Number"
}); |
Your new changes look good. Can you add a call to |
Ah okay, just added the |
Great! Thanks for the PR. |
Oh no problem, thanks for helping me get this implemented! |
This is released in version 2.8.1 if you want to use it. |
Wrote this after reading through the discussion here: #484 #488
Main point of concern is that this is throwing an error, I've seen other functions call
this.emit('error', ...)
instead but wasn't sure if it was appropriate here. Since it's programmer error, and isn't really meant to be "caught", I think it should be okay, but open to feedback. There's a doc on Joyent's site that goes more into detail: https://www.joyent.com/developers/node/design/errors (specifically the section titled "(Not) handling programmer errors")