-
Notifications
You must be signed in to change notification settings - Fork 7
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
to throw or not to throw #9
Comments
cc @benjamingr any thought? |
Some notes:
The "throw for callback reject for promise" depends, if you are sure that it is a programmer error you can throw, otherwise it's better to use the Since there is no unhandled error detection for callback |
If you have the references somewhere I'll be very interested.
True for promise but it does release Zalgo when the "hybrid" function is used with a callback. 'use strict'
var pg = require('polygoat')
function delay (time, cb) {
return pg(function (done) {
if (typeof time !== 'number') {
return cb(new Error(time + ' is not a number'))
}
setTimeout(done, time)
}, cb)
}
delay('123', function (err) {
console.log(err)
})
console.log('w00t') prints
|
is there a consensus on whether promise instantiation may/should throw? for example delay('123') // throws like in my "throw" approach example above or maybe a interesting discussion on that topic somewhere? couldn't find anything |
nodejs/docs#82
That's fair enough.
Promise creation shouldn't throw, it should reject, I can dig the discussion up if it's really important to you from the WHATWG mailing list when promises were introduced to the DOM. Note that in one very clear case - the promise constructor itself - passing invalid arguments does throw. I think this is because unhandled rejection detection didn't really exist back then. |
I've updated the "throw for callback, reject for promise" approach with a much more convenient approach |
thanks for the links, agree with you
True, updated the "throw" approach
I'm all for choice but yeah I guess it would make everybody's life easier if all agreed on that. Okay, discarded the approach "throw". That leaves us with the "callback" and the "throw for callback, reject for promise" approaches. I'd like to add a "error handling" section in the README.md of polygoat and I'm not sure which one (or both?) I should recommend. I guess you're favorite would be the "callback" approach. Should polygoat dezelago itself for the "callback" approach? dezalgo module uses asap and code base is pretty big :/ I guess polygoat could simply use setImmediate || process.nextTick || postMessage || setTimeout OR use dezalgo on Node.js anything else available on browser (so that the browser file size doesn't increase with dezalgo source), the dezalgo function could also be provided as an optional parameter like the Promise impl |
here is a proposal #10 |
Scheduling seems like a pretty nice task until you realize just how many schedulers you have to support to do a reasonable job. If you want to "dezalgo" I'd do it by |
the point of polygot is that it doesn't require Promise support/polyfill |
@sonnyp right, but sometimes you don't have a choice and native promises are available and you have to use them to dezalgo. Ridiculous? Sure - just not as bad as users in iOS 8.3 not being able to use your code since timers are implicitly throttled in low battery mode and/or background. Fun. |
setTimeout/postMessage is an other option Yes, it's not as fast but in the browser who cares? |
setTimeout sometimes doesn't get executed in some browsers (above I name iOS 8.3 in a background tab as an example). It isn't slow (it is, but that's not the problem) - it breaks the functionality of the code. |
I think it's best when in doubt to not assume something is a programmer error. Polygoat could punt the question to library authors. If they do this: function something(input, cb) {
...code that might throw...
return pg(function (done) {
eventuallyCall(done);
}, cb)
} ...then they get the "throw for callback, reject for promise" behavior. Otherwise: function something(input, cb) {
return pg(function (done) {
...code that might throw...
eventuallyCall(done);
}, cb)
} ...then they get the "callback" behavior. In any case I agree if/whenever a callback is called, whether or not with an error, it should never happen before the current call stack bottoms out. |
Your first example would throw for callback and for promise If you want the callback version to throw, throw inside Regarding dezalgo, not sure it should be in the scope of this module. |
I see
32 different approachescallback
Pros
Cons
return cb
Should polygoat dezalgo itself?
throw for callback, reject for promise
Pros
Cons
throwEDIT: discard, promise creation shouldn't throw
Pros
Cons
The text was updated successfully, but these errors were encountered: