javascriptasynchronousasync-awaitecmascript-2017

Reuse object literals created via destructuring


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


Solution

  • missing semicolon

    You have two (...) sequenced together -

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

    ES interpreter sees this as -

    await deployApi.createBucket(options)(... await deployApi.deleteBucket(options.branch))
    

    Which is equivalent to -

    const r1 = deployApi.createBucket(options)
    const r2 = r1(... await deployApi.deleteBucket(options.branch))
    await r2
    

    Which is very different than your actual intention -

    const r1 = await deployApi.createBucket(...)
    const r2 = await deployApi.deleteBucket(...)
    

    To reuse the let destructured object, the parentheses are required -

    // 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 } = ...)
    

    If you reuse the same let bindings, the required parentheses change the meaning of your program when the semicolon is not used.

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