Reuse object literals created via destructuring

2019-07-30 05:29发布

I am trying to reuse the object literals for both async calls. At the end my expect should check the success of the deleteBucket call. Problem is I can't do this, or it says I've got dup variables defined:

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

Someone told me I could put a () around the second, but that's bombing out giving me a TypeError: (0 , _context4.t0) is not a function error:

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

This does work, but requires me to change the names of the resolved objects which I do not want to do:

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

UPDATE:

someone suggested I needed a semi colon after the const line. Didn't make any difference, I still get the same error when I run it:

enter image description here

1条回答
啃猪蹄的小仙女
2楼-- · 2019-07-30 05:44

You don't have to change the names. There's probably just something wrong somewhere else in your program

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


oh I see, you're missing a semicolon!

which explains the "TypeError: (0 , _context4.t0) is not a function" you were seeing – not much else you can do here; I know semicolons suck, but you'll have to use one in this specific scenario.

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

Yes it does; try running the exact same code snippet I had above, but without semicolons – you'll recognise the familiar 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)

查看更多
登录 后发表回答