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

时间:2017-06-10 04:01:49

标签: javascript asynchronous async-await ecmascript-2017

我正在尝试为两个异步调用重用对象文字。最后,我期望检查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线之后需要一个半冒号。没有任何区别,我运行时仍然会遇到同样的错误:

enter image description here

1 个答案:

答案 0 :(得分:3)

您不必更改名称。你的程序中的其他地方可能只是出了点问题



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)不是你看到的函数" - 你在这里做的事情不多;我知道分号很糟糕,但你必须在这个特定情况下使用分号。

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

"它没有什么区别"

是的,是的;尝试运行我上面完全相同的代码段,但没有分号 - 你会认识到熟悉的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)