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

Pos*_*Guy 3 javascript asynchronous async-await ecmascript-2017

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

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
    })
Run Code Online (Sandbox Code Playgroud)

有人告诉我我可以在第二个周围放一个 () ,但这会导致错误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
})
Run Code Online (Sandbox Code Playgroud)

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

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
    })
Run Code Online (Sandbox Code Playgroud)

更新:

有人建议我在 const 行后面需要一个分号。没有什么区别,当我运行它时仍然遇到同样的错误:

在此输入图像描述

Tha*_*you 6

缺少分号

你有两个(...)一起测序 -

await deployApi.createBucket(options)  

({ failure, success, payload} = await deployApi.deleteBucket(options.branch))
Run Code Online (Sandbox Code Playgroud)

ES 解释器将其视为 -

await deployApi.createBucket(options)(... await deployApi.deleteBucket(options.branch))
Run Code Online (Sandbox Code Playgroud)

这相当于 -

const r1 = deployApi.createBucket(options)
const r2 = r1(... await deployApi.deleteBucket(options.branch))
await r2
Run Code Online (Sandbox Code Playgroud)

这与你的实际意图有很大不同 -

const r1 = await deployApi.createBucket(...)
const r2 = await deployApi.deleteBucket(...)
Run Code Online (Sandbox Code Playgroud)

要重用let解构的对象,需要括号 -

// initial assignment
let { a, b, c } = ...

// without the parentheses, ES interprets as illegal assignment using =
{ a, b, c } = ...

// with parentheses, ES interprets as destructuring assignment
({ a, b, c } = ...)
Run Code Online (Sandbox Code Playgroud)

如果重复使用相同的let绑定,则在不使用分号时,所需的括号会更改程序的含义。

it('can delete a bucket', async () => {
  const options = { branch: '11' }
                                                           // semicolon here
  let { failure, success, payload } = await deployApi.createBucket(options); 
  
  ({ failure, success, payload} = await deployApi.deleteBucket(options.branch))
  
  expect(success).to.be.true
})
Run Code Online (Sandbox Code Playgroud)