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?