setTimeOut和Promise执行顺序#105
Anuluca Date : 2021-03-03 Tags : 2
Promise先执行于setTimeout,因为setTimeout本身是异步请求,而Promise是直接执行的,只有后面的then才是异步执行(但也在Promise内的内容执行完成后直接执行)
示例:
问题:以下输出顺序多少?
1 2 3 4 5 6 7 8 9 10 11
| setTimeout(() => console.log(0)) new Promise((resolve) => { console.log(1) resolve(2) console.log(3) }).then(o => console.log(o))
new Promise(resolve => { console.log(4) resolve(5) }).then(o => console.log(o)).then(() => console.log(6))
|
答案:
1
| 1 => 3 => 4 => 2 => 5 => 6 => 0
|