簡體   English   中英

從 function 返回的數據是 Promise(已完成)數據

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM