Chr*_*rez 6 javascript asynchronous async-await es6-promise
尝试显示 json 中的数据。\n当我在控制台上运行函数时,它显示:
\nPromise\xc2\xa0{}和[[PromiseState]]:“已完成”
\n但实际上并没有返回任何东西。
\ngetJobs = async () => {\ntry{\n response = await fetch("./data.json");\n const data = await response.json();\n return data;\n} catch(error){\n console.log('Error getting data', error)\n } \n} \n\n\nrenderJobs = async () => {\n const jobs = await getJobs();\n const view = `\n <div class='card-company'>\n ${jobs.map(companyName => `\n <p>${companyName.company}</p>\n `).join('')}\n </div>\n `\n return view\n}\nRun Code Online (Sandbox Code Playgroud)\n我在属性内拥有所需的所有p[[PromiseResult]]标签,但文档中没有显示任何内容。
我还没有找到这样的东西,如果这个片段有什么需要改进的地方,我很感激你的反馈。
\n谢谢。
\n对我来说,这似乎是一个不合适的模板。无论如何,我认为在这种情况下它没有用处。应该更像是:
const M = tag=>document.createElement(tag);
const renderJobs = async () => {
const jobs = await getJobs(), frag = document.createDocumentFragment();
let d;
for(let o of jobs){
d = M('div'); d.className = 'card-company'; d.textContent = o.company;
frag.appendChild(d);
}
// you could append frag here or return and append later
return frag;
}
Run Code Online (Sandbox Code Playgroud)