[英]converting Promise to async await syntax problem
I want to convert promise in to async await but I cannot find the right syntax for the function that reads the data from the response我想将 promise 转换为异步等待,但我找不到从响应中读取数据的 function 的正确语法
export async function getDataFromServer(id) { try { const resp = await fetch('/dataget', { credentials: "same-origin", mode: "same-origin", method: 'POST', headers: { "Content-Type": "application/json" }, body: `${id}` }) const data = await resp.json() const handel = await function (data) { console.log(data); // render function renderDataFromServer(data) } } catch (err) { if (err === "server") return console.log(err) } }
If you remove the unnecessary function declaration it should work:如果您删除不必要的 function 声明,它应该可以工作:
export async function getDataFromServer(id) {
try {
const resp = await fetch('/dataget', {
credentials: "same-origin",
mode: "same-origin",
method: 'POST',
headers: { "Content-Type": "application/json" },
body: `${id}`
})
const data = await resp.json();
console.log(data);
// render function
return renderDataFromServer(data);
} catch (err) {
if (err === "server") return
console.log(err)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.