-
-
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
any idea to re-use the same error variable? #11
Comments
@pygy taught me this, but JS already supports updating a previous destructured binding let [a,b] = [1,2]
console.log(a,b)
// logs 1,2
void ([a,b] = [4,5])
console.log(a,b)
// logs 4,5 So you'd just do the same thing: async function getData() {
let [err, response] ?= await fetch(
"https://api.example.com/data"
)
if (err) {
handleRequestError(err)
return
}
void ([err, json] ?= await response.json())
if (err) {
handleParseError(err)
return
}
[...]
} |
@JAForbes is it necessary to put ‘void (…)’ around the second assignment? |
@BThero nope, if you use semi-colons carefully you can skip the parens and the void let [a,b] = [1,2];
console.log(a,b);
// logs 1,2
[a,b] = [4,5];
console.log(a,b);
// logs 4,5 Just be careful, starting a new line with |
Back to async function getData() {
var [err, response] ?= await fetch('https://api.example.com/data');
if (err) return handleRequestError(err);
var [err, json] ?= await response.json();
if (err) return handleParseError(err);
// ...
} |
@JAForbes Ahh that's great. Yet another reason to enforce semicolons 😄 Thanks for the detailed explanation! |
Maybe this is a valid reason to use the third approach try [error, response] = await fetch('url')
if(err) { return 'error' }
try [error, json] = await response.json() |
Reusing error variables is super confusing. I don't ever see this becoming a thing. |
It doesn't fit all the scenarios, but you can use labeled statements in some cases: fizz: {
const [ error, result ] ?= fnA();
...
}
buzz: {
const [ error, result ] ?= fnB();
...
} But I can't see a practical reason for this since that we can just vary the variable name in the Iterable. |
True, or even just unlabeled blocks: {
const [ error, result ] ?= fnA();
...
}
{
const [ error, result ] ?= fnB();
...
} |
I would prefer to use |
Prerequisites
Versions
n/a
A minimal reproducible example
n/a
Description
Can you think of some way to use the same error variable, something like this (I know it doesn't work like this, it's just an example)
Steps to Reproduce
n/a
Expected Behavior
No response
The text was updated successfully, but these errors were encountered: