Appearance
promise 那些事
基础
浏览器提供了一个queueMicrotask 可以执行微任务(Microtasks) mdn传送门
js
//某大厂面试题
Promise.resolve().then(() => {
console.log(0);//第1层
return Promise.resolve(4)
}).then((res) => {
console.log(res)//第2层
})
Promise.resolve().then(() => {
console.log(1);//第1层
}).then(() => {
console.log(2);//第2层
}).then(() => {
console.log(3);//第3层
}).then(() => {
console.log(5);//第4层
}).then(() =>{
console.log(6);//第5层
})
js
//进阶题目
Promise.resolve().then(() => {
console.log(0);//2
return new Promise(resolve => {
console.log("newPromise")
return resolve(4) //2
}).then(res => {
console.log('新增的 then 执行啦!')
// Promise.resolve().then(()=>{
// console.log("新增测试")
// })
return res
})
}).then(res => {
console.log(res)
})
Promise.resolve().then(() => {
console.log(1);//2
}).then(() => {
console.log(,2);//3
}).then(() => {
console.log(3);//4
}).then(() => {
console.log(5);//5
}).then(() => {
console.log(6);//6
})