<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>promise</title>
</head>
<body>
<script>
let p = new Promise((resolve, reject) => {
resolve('done')
})
const res = p.then(value => {
console.log(value)
}, reason => {
console.warn(reason)
})
console.log(res)
</script>
</body>
</html>
为什么控制台先打印res再打印value呢?代码都是同步的,不应该从上往下依次执行嘛?
谁说是同步……如果是同步就不需要 Promise 了。看看这些,区别理解同步和异步等待
尤其是这两篇: