-
-
Notifications
You must be signed in to change notification settings - Fork 16
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
re: Why Not data First? #13
Comments
Error first for callbacks makes sense because you always want to check the error, and parameters after the first one are not guaranteed. Error first in the proposal is very odd, in particular because the pattern it is emulating doesn't do error first. So it will be confusing for people working in multiple languages. Thus, it will be a source of friction. But it's also weird without considering any other languages: // As proposed:
const [error, data] = await something()
// But if we insist on being silly and ignoring the error:
const [ , data] = await something()
// Switching to error second:
const [data, error] = await something()
// And that silly case again:
const [data] = await something() In other words, as proposed, I can envision code bases littered with ugle |
Having Ignoring an error: const val = await fn()
// this throws if fn rejects Suppressing the error: const [, data] = await fn()
const val = fn().catch(() => null)
// this simply does nothing and makes val undefined if fn throws. From the What This Proposal Does Not Aim to Solve section of this proposal:
Completely suppressing errors is totally different from just ignoring the possibility of a rejection and the handle needed in case it rejects. // this gets data in a simpler syntax and throws if an error happens
const data = fn()
// This is WORSE because it simply makes the error disappear.
const [data] ?= fn() And forgetting to add a Besides that, it also follows an already present syntax in callbacks, where the |
I addressed that at the top of my comment.
|
why not legible? |
This, but isn't it better to make ignoring the error harder? If you deliberately want to ignore it, do not use this operator or maybe not babysit and have data as first? I had the same thought too, somehow seemed clearer and could just follow Go to make jumping languages easier. Why make superficial changes without much point? Just keep it same... Eslint can have a rule to warn about not handling error or otherwise it's a mistake to use this operator. No point to reason adjusting something avoids mistakes. |
IMHO data-first approach is simply better for both readability and usage. The ability to ignore an error could be a valuable feature. |
The
To me the intention of the code is therefore clear, the original author is making the choice to discard the not use the first value. |
Makes perfect sense, I'll try to add this on the spec, although i didn't want to facilitate suppressing errors in any way, but since this proposal uses errors as values, i guess there's nowhere else to go. |
I can't, since doing |
Also, for functions that returns const [error] ?= await sendDataToServer(data);
if (error) {
console.warn('oops')
}
// success! |
This is a fair argument. |
I would like to contribute to the discussion and add that in functional programming (languages and libraries afaik) like fp-ts and effect.ts by convention dictates that Left is used for failure and Right is used for success. Ref: |
consider the following branch of discussion: #4 (comment) |
I believe there's another reason to stick to error first in js, and that's for language consistency in Nodejs, the most used JS server runtime. When using callback functions, the convention is to use an error first pattern. Like node's new glob util:
Mapping that to the following feels almost natural.
I think the error-first callback pattern is popular enough, even in libraries, to support this case of error first, and it might be worth mentioning in the proposal.
The text was updated successfully, but these errors were encountered: