js中的同步和异步

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function a() {
window.fetch("http://101.43.187.22:9501/api/nav/wallPaper").then(async response => {
const resp = await response.json()
console.log(resp)
return resp
})
console.log("a")
}
function b() {
console.log("b")
}

a()
b()

// a
// b
// {code: 200, msg: null, result: Array(8)}

a()b() 是同步执行,a()fetch请求是异步操作。