fetch("https://jsonplaceholder.typicode.com/posts").then(
(response) => {
console.log(response.json()); //why the data not logged and promise is logged
}
);
but work if i writte
let response = fetch("https://jsonplaceholder.typicode.com/posts")
.then((response) => response.json())
.then((data) => console.log(data));
為什么沒有記錄資料并在第一個代碼中記錄了承諾?
uj5u.com熱心網友回復:
response.json()正在回傳一個承諾,因此您需要通過執行第二個示例中的操作或使用asyncand來等待它await:
fetch("https://jsonplaceholder.typicode.com/posts").then(
async (response) => {
console.log(await response.json());
}
);
js 小提琴:
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/418263.html
標籤:
