-
Notifications
You must be signed in to change notification settings - Fork 96
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
optional callback #55
Conversation
this makes the callback more optional by preventing an error when `once()` calls `apply()` on the unregistered (meaning non-existent) callback, i.e. not passed to the exported xhr() constructor
Nice! |
No. callbacks are not optional, when doing IO errors have to be handled. We should throw an exception when you do not pass a callback. Edgecase: when opting into sync IO the callback is optional. |
Yep I had a feeling you would not want optional callbacks. And I tend to agree. However, the callback here remains a requirement, though it may not be explicit for users when the library's noop satisfies the required callback function placement as a template. Before you revert, consider the example set by the levelup API. When we put a new record into a leveldown backend the err callback is not required. Yet this amazing storage API handles the write. That is why ultimately this is a trust issue. As a user I trust the levelup API just as I trust xhr in the browser. User belief in the API is enough to handle the need for extra characters written as the callback. And beyond that trust, Node just basically garbage collects whatever that entails. This is a win for convenience and its use case is the need to fire and forget. |
https://github.com/rvagg/node-levelup/blob/master/lib/util.js#L75-L79 function dispatchError (levelup, error, callback) {
return typeof callback == 'function'
? callback(error)
: levelup.emit('error', error)
} Having an optional callback and falling back to emitting an error is fine. Having an optional callback and falling back to a noop is terrible. Since there is no event emitter to emit an error on we have no choice but to make the callback required. I would be slightly more open to Changing the interface in a way where its possible for a developer to have a bug of forgetting the callback is not acceptable. |
I can't argue that type of logic. I'll update this with a |
@Raynos what do you think about a if set to this way we don't change the interface and have to be deliberate about not passing the callback |
there are occasions when i dont care about server responses to
xhr()
when the server response doesn't matter but you still want to fire-and-forget some type of request, then the callback ought to be optional:
this makes the callback more optional by preventing an error when
once()
callsapply()
on the unregistered (meaning non-existent)callback, i.e. not passed to the exported xhr() constructor.