[英]Returned data from function is Promise (fulfilled) data
defaultValue = getFile(getCurrent.value[name])
Function 獲取文件:
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
}
如果我 console.log(defaultValue): 我看到以下 output;
但我需要一個數組而不是這個 Promise 東西。 怎么解決?
您必須在getFile
上使用await
關鍵字來檢索值前 promise 因為您的 getFile 返回 Promise。
async function yourFunc() {
defaultValue = await getFile(getCurrent.value[name])
// do whatever you want
}
您需要使用await
關鍵字
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 }]
}
然后也等待對getFile
的調用
defaultValue = await getFile(getCurrent.value[name])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.