重用通过解构创建的对象字面量

Reuse object literals created via destructuring

我正在尝试为两个异步调用重用对象文字。最后,我的期望应该检查 deleteBucket 调用是否成功。问题是我不能这样做,或者它说我已经定义了重复变量:

it('can delete a bucket', async () => {
      const options = { branch: '11' }

      let { failure, success, payload } = await deployApi.createBucket(options)
      let { failure, success, payload} = await deployApi.deleteBucket(options.branch)

      expect(success).to.be.true
    })

有人告诉我我可以在第二个附近放一个 () ,但这给了我一个 TypeError: (0 , _context4.t0) is not a function 错误:

it('can delete a bucket', async () => {
  const options = { branch: '11' }

  let { failure, success, payload } = await deployApi.createBucket(options)

  ({ failure, success, payload} = await deployApi.deleteBucket(options.branch))

  expect(success).to.be.true
})

这确实有效,但需要我更改我不想做的已解析对象的名称:

it('can delete a bucket', async () => {
      const options = { branch: '11' }

      let { failure, success, payload } = await deployApi.createBucket(options)
      let { failure1, success1, payload1} = await deployApi.deleteBucket(options.branch)

      expect(success1).to.be.true
    })

更新:

有人建议我在 const 行之后需要一个分号。没有任何区别,当我 运行 时我仍然得到同样的错误:

您不必更改名称。您的程序中的其他地方可能有问题

let {x,y,z} = {x: 1, y: 2, z: 3};

console.log(x,y,z);
// 1 2 3

({x,y,z} = {x: 10, y: 20, z: 30});

console.log(x,y,z);
// 10 20 30


哦,我明白了,你少了一个分号!

这解释了您所看到的 "TypeError: (0 , _context4.t0) is not a function" – 您在这里无能为力;我知道分号很糟糕,但在这种特定情况下您必须使用分号。

// missing semicolon after this line!
let { failure, success, payload } = await deployApi.createBucket(options); // add semicolon here!

// without the semicolon, it tries to call the above line as a function
({ failure, success, payload} = await deployApi.deleteBucket(options.branch))

"It doesn't make a difference"

是的,确实如此;尝试 运行 我上面的完全相同的代码片段,但没有分号 - 你会认出熟悉的 TypeError

let {x,y,z} = {x: 1, y: 2, z: 3}

console.log(x,y,z)
// 1 2 3

({x,y,z} = {x: 10, y: 20, z: 30})
// Uncaught TypeError: console.log(...) is not a function

console.log(x,y,z)