defaultValue = getFile(getCurrent.value[name])
函式獲取檔案:
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): 我看到以下輸出; 
但是我需要一個陣列而不是這個 Promise 的東西。怎么解決?
uj5u.com熱心網友回復:
您需要使用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])
uj5u.com熱心網友回復:
您必須使用await關鍵字getFile來檢索價值前端承諾,因為您的 getFile 回傳 Promise。
async function yourFunc() {
defaultValue = await getFile(getCurrent.value[name])
// do whatever you want
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/359715.html
標籤:javascript 承诺
上一篇:“反應”串列中的每個孩子都應該有一個唯一的“關鍵”道具
下一篇:我想讓它用一個功能多次作業
