1
我有一個預取反應應用程序資源的問題。我有一個函數,它應該獲取json資源並將它們分配給循環中的對象屬性。然後該函數應該返回已經解析完jsons的對象,而不是返回具有未解析promise的屬性的對象。阻止js執行,直到異步請求完成
是否有可能停止執行js代碼,直到承諾fullfiled所以該函數將返回正確的數據?
是的,就像是一個同步請求。
async function getJson (file, url) {
const response = await fetch(`${url}/${file}.json`);
const json = await response.json();
return json;
}
async function getTranslations(files, url) {
let r = {};
files.map((file, i) => {
r[file] = await getJson(file, url);
});
return r;
}