【发布时间】:2018-03-23 17:46:21
【问题描述】:
我在js中有一些简单的Promise:
let link = "https://jsonplaceholder.typicode.com/posts/1";
fetch(link)
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
...我想把它翻译成'async await',就像这里:
const API_ENDPOINT = 'https://jsonplaceholder.typicode.com/posts/1';
const fetchUsers = async () => {
const response = await fetch(API_ENDPOINT);
const data = await response.json();
};
...但我不知道如何正确地将其包含在“异步等待”中。这涉及到错误:
fetchUsers()
.then(response => response.json())
.then (data => console.log(data))
.catch(error => console.error(`ERROR ${error}`));
你能帮我解决这个错误吗?
【问题讨论】:
-
什么错误?
-
看起来您现在正尝试连续两次呼叫
.json()
标签: javascript promise async-await