我对这段代码有一些疑问,为什么下面的代码在完成 fetchData 请求之前打印“end”。 JavaScript 不是异步的。那么为什么在完成 fetchdata 之前运行 end 方法
function start(){
console.log("start")
}
function fetchData(){
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(json => console.log(json))
}
function end(){
console.log("end")
}
start()
fetchData()
end()
最佳答案
您的 fetchData() 函数是异步的。查看 MDN 上的码头.
The Fetch API provides a JavaScript interface for accessing and manipulating parts of the HTTP pipeline, such as requests and responses. It also provides a global fetch() method that provides an easy, logical way to fetch resources asynchronously across the network.
关于javascript - 为什么下面的代码是异步执行的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57215044/