【发布时间】:2020-05-11 17:52:45
【问题描述】:
我的一个服务器调用花了将近 30 秒来返回数据,所以它总是未定义,所以我使用异步并承诺解决此问题,但得到“未定义”。以下是我的代码片段提前谢谢
function fetchFunc() {
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then((json) => {
// console.log(json)
return json;
})
}
function resolveAfter2Seconds() {
return new Promise(resolve => {
resolve(fetchFunc());
});
}
async function asyncFunc() {
debugger
console.log("res" + resolveAfter2Seconds())
let response = await resolveAfter2Seconds();
console.log("response = " + response);
}
asyncFunc();
【问题讨论】:
-
你需要返回 fetch
-
fetchFunc()不返回任何内容
标签: javascript async-await es6-promise