1
defaultValue = getFile(getCurrent.value[name]) 

Function getFile:

const getFile = async (id) => {
  const data = store.dispatch(Actions.FILE, id).then(() => {
    const data = [{ description: store.getters.currentFile.description, url: 'https://test.com/uploads/'+ store.getters.currentFile.name }]
    return data
  })
  return data
}

If I console.log(defaultValue): I see the following output; enter image description here

But I need a array instead of this Promise thing. How to solve?

nhatimme
  • 383
  • 3
  • 19

2 Answers2

1

You have to use await keyword on your getFile to retrieve value front promise because your getFile returns Promise.

async function yourFunc() {
   defaultValue = await getFile(getCurrent.value[name])
   // do whatever you want
}
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
Radonirina Maminiaina
  • 6,958
  • 4
  • 33
  • 60
  • This will make no difference... the code inside `getFile` never waits for the response to be received and nothing is ever `await`ed within it – Jamiec Nov 18 '21 at 09:51
  • @Jamiec The current code inside `getFile` is returning a Promise that resolves to an array of objects, so the code witihn getFile itself doesn't need to wait for the response, as the Promise being returned will be notified/resolve when the response is received, so this code should work without changing `getFile` – Nick Parsons Nov 18 '21 at 10:14
  • 1
    @NickParsons you're absolutely right.. the thrice reuse of the variable `data` confused me – Jamiec Nov 18 '21 at 10:16
1

You need to use the await keyword

const getFile = async (id) => {
   await store.dispatch(Actions.FILE, id);
   return  [{ description: store.getters.currentFile.description, url: 'https://test.com/uploads/'+ store.getters.currentFile.name }]
}

and then await the call to getFile too

defaultValue = await getFile(getCurrent.value[name]) 
Jamiec
  • 133,658
  • 13
  • 134
  • 193