Reuse object literals created via destructuring

做~自己de王妃 提交于 2019-12-11 06:07:21

问题


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:


回答1:


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)


来源:https://stackoverflow.com/questions/44469366/reuse-object-literals-created-via-destructuring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!