0

I have this:

function test1() {
   return new Promise(resolve => {
      return resolve({
         a: 1
      })
   })
}

function test2() {
   return new Promise(resolve => {
      return resolve({
         a: 2
      })
   })
}

async function a() {
   let { a } = await test1()
   console.log(a) // 1
   let { a } = await test2()
   console.log(a)
}

a()

But got this error, obviously:

error: unknown: Identifier 'a' has already been declared (20:9)

  18 |    let { a } = await test()
  19 |    console.log(a) // 1
> 20 |    let { a } = await test1()
     |          ^
  21 |    console.log(a)
  22 | }
  23 | 

As you can see I want to avoid this:

async function a() {
   let results = await test1()
   let { a } = results
   console.log(a) // 1
   results = await test2()
   a = results.a
   console.log(a) // 2
}

Because I actually cannot redeclare the same param name with the same let. And I also cannot do this:

{ a } = await test2();

Because a declaration is missing.

How to make this efficient and easy to read and yet keep the efficient ES6 functionality?

Raz Buchnik
  • 7,753
  • 14
  • 53
  • 96

3 Answers3

2

You just need a different variable name. You could put it into an a1 variable the first time, and an a2 variable the second time:

function test1() {
   return new Promise(resolve => {
      return resolve({
         a: 1
      })
   })
}

function test2() {
   return new Promise(resolve => {
      return resolve({
         a: 2
      })
   })
}

async function a() {
   let { a: a1 } = await test1()
   console.log(a1) // 1
   let { a: a2 } = await test2()
   console.log(a2)
}

a()

Could also overwrite the old a, but reassigning variables when not essential should probably be avoided:

function test1() {
   return new Promise(resolve => {
      return resolve({
         a: 1
      })
   })
}

function test2() {
   return new Promise(resolve => {
      return resolve({
         a: 2
      })
   })
}

async function a() {
   let { a } = await test1()
   console.log(a); // 1
   ({ a } = await test2());
   console.log(a)
}

a()
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • Why should it be avoided? Sometimes there cases you take the variable, pass it to some function, get it back and the new instance of this variable should be sent to another function - after the modification. Could you please explain why need to avoid such a thing? – Raz Buchnik Apr 05 '20 at 10:16
  • 2
    Because reassigning a variable unnecessarily results in unnecessary cognitive overhead. For the same reason, it's good to prefer `const` instead of `let` - `let` should be avoided unless you *have* to reassign. Eg, consider you have 100 lines of code. On line 10, you do `const someVar = 'foo';`. Compare to `let someVar = 'foo'`. Then on line 95, you reference `someVar`. What does `someVar` refer to? If you use `const`, you'll immediately know that it's `'foo'`. But if you use `let`, you'll have to carefully pick through the 90 lines of code in between to see if you reassigned. – CertainPerformance Apr 05 '20 at 10:19
1

You can rename the property variable

const x = {a: 1};
const y = {a: 2};

const {a: foo} = x;
const {a: bar} = y;

console.log(foo, bar);
baao
  • 71,625
  • 17
  • 143
  • 203
0

You cant do that, I suggest an array of a's will work in your case:

function test1() {
   return new Promise(resolve => {
      return resolve({
         a: 1
      })
   })
}

function test2() {
   return new Promise(resolve => {
      return resolve({
         a: 2
      })
   })
}

async function a() {
   let as = []
   as.push(await test1())
   console.log(as.pop()); // 1
   as.push(await test2());
   console.log(as.pop())
}

a()
Nilanka Manoj
  • 3,527
  • 4
  • 17
  • 48