【Promise第一题】
参考答案
基础结论
const p = new Promise(r => { console.log(1); r(); });
p.then(() => console.log(2));
console.log(3);
// 1 3 2
- executor 同步 → 1
- then 回调微任务 → 2 延后
- 同步 log → 3
即"同步执行器 → 同步代码 → 微任务 then"。这是所有 Promise 顺序题的基础。
【Promise第一题】
const p = new Promise(r => { console.log(1); r(); });
p.then(() => console.log(2));
console.log(3);
// 1 3 2
即"同步执行器 → 同步代码 → 微任务 then"。这是所有 Promise 顺序题的基础。
本题来自 计算机知识面试题库,共 921 道题。