Skip to content
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

Open
2 tasks done
opensas opened this issue Aug 16, 2024 · 10 comments
Open
2 tasks done

any idea to re-use the same error variable? #11

opensas opened this issue Aug 16, 2024 · 10 comments

Comments

@opensas
Copy link

opensas commented Aug 16, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

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)

async function getData() {
  const [err, response] ?= await fetch(
    "https://api.example.com/data"
  )

  if (err) {
    handleRequestError(err)
    return
  }

  const [err, json] ?= await response.json()

  if (err) {
    handleParseError(err)
    return
  }
  [...]
}

Steps to Reproduce

n/a

Expected Behavior

No response

@JAForbes
Copy link

@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
  }
  [...]
}

@BThero
Copy link

BThero commented Aug 16, 2024

@JAForbes is it necessary to put ‘void (…)’ around the second assignment?

@JAForbes
Copy link

@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 [...] without a semi colon on the previous expression will be treated as a property access, and starting a new line with (...) will be interpreted as a function call.

@smeijer
Copy link

smeijer commented Aug 16, 2024

Back to var 😅

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);

  // ...
}

@BThero
Copy link

BThero commented Aug 16, 2024

@JAForbes Ahh that's great. Yet another reason to enforce semicolons 😄

Thanks for the detailed explanation!

@arthurfiorette
Copy link
Owner

Maybe this is a valid reason to use the third approach try (as using), where the try operator would allow the overload of the error variable.

try [error, response] = await fetch('url')

if(err) { return 'error' }

try [error, json] = await response.json()

@arthurfiorette
Copy link
Owner

Reusing error variables is super confusing. I don't ever see this becoming a thing.

@felippe-regazio
Copy link

felippe-regazio commented Aug 22, 2024

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.

@JAForbes
Copy link

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();
  ...
}

@khaosdoctor
Copy link

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 try as using in this case over separated scopes, aesthetically speaking. But I think (and as it was stated on #4) this implies a different handling of the variable which could be a bigger scope, so I'd go with parallel assignments in this one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants