考察事件循环、任务队列、和setTimeout、Promise结合,以输出结果顺序的方式来考察
范例 出处
setTimeout(() => console.log(1), 0); new Promise(resolve => { console.log(2); resolve(); }).then(() => { console.log(3); }); console.log(4);
范例 出处
setTimeout(()=> console.log(1)) new Promise(resolve => { resolve() console.log(2) }).then(_ => { console.log(3) Promise.resolve().then(_ => { console.log(4) }).then(_ => { Promise.resolve().then(_ => { console.log(5) }) }) }) console.log(6)
范例 出处
const p = new Promise((resolve, reject) => { console.log(1); setTimeout(() => { console.log(2); }) resolve(1); }).then(res => { console.log(3); }) setTimeout(() => { console.log(4); }) console.log(5);
范例 来自面经 第5题
async function async1(){ console.log('async1 start') await async2() console.log('async1 end') } async function async2(){ console.log('async2') } console.log('script start') setTimeout(function(){ console.log('setTimeout') },0) async1() new Promise(function(resolve){ console.log('promise1'); resolve() }).then(function(){ console.log('promise2') }) console.log('script end')
范例 来自面经
const pro = new Promise((resolve, reject) => { const innerpro = new Promise((resolve, reject) => { setTimeout(() => { resolve(1); }, 0); console.log(2); resolve(3); }); innerpro.then(res => console.log(res)); resolve(4); console.log("pro"); }) pro.then(res => console.log(res)); console.log("end");
范例 来自这里
console.log('begin') setTimeout(() => { console.log('setTimeout 1') Promise.resolve().then(() => { console.log('promise 1') setTimeout(() => { console.log('setTimeout2 between promise1&2') }) }).then(() => { console.log('promise 2') }) }, 0) console.log('end')
范例 题目出处
function arr(){ setTimeout(function(){ console.log('1'); },0) async function async1(){ console.log('4'); await async2(); console.log('6'); } async function async2(){ console.log('5'); } async1(); new Promise(function(resolve,reject){ console.log('2'); resolve(); }).then(function(e2){ console.log('h'); }) console.log('3'); } arr()
范例5 题目出处
setTimeout(_ => console.log(1)) new Promise(resolve => { resolve() console.log(2) }).then(_ => { setTimeout(_ => console.log(3)) console.log(4) Promise.resolve().then(_ => { console.log(5) }).then(_ => { Promise.resolve().then(_ => { console.log(6) }) }) }) console.log(7)
范例 题目出处
console.log(1); setTimeout(() => { console.log(2); Promise.resolve().then(() => { console.log(3) }); }); new Promise((resolve, reject) => { console.log(4) resolve() }).then(() => { console.log(5); }) setTimeout(() => { console.log(6); }) console.log(7);
范例 题目出处
setTimeout(() => { console.log(1) }, 0); new Promise((res)=>{ console.log(2);res() }).then(()=>{ console.log(4) }) console.log(3)
范例 题目出处
const a = () => { new Promise((resolve) => { resolve(1) console.log('a') }).then(() => { console.log('b') }) } a() console.log('c')
范例 题目出处
new Promise((resolve, reject) => { reject(1) }).catch(() => { console.log(2); }).then(() => { console.log(3) }, (v) => { console.log(v) }) console.log(4)
范例 题目出处
async function async1() { console.log('async1 start'); await async2(); console.log('async1 end'); } async function async2() { console.log('async2 start'); return new Promise((resolve, reject) => { resolve(); console.log('async2 promise'); }) } console.log('script start'); setTimeout(function() { console.log('setTimeout'); }, 0); async1(); new Promise(function(resolve) { console.log('promise1'); resolve(); }).then(function() { console.log('promise2'); }).then(function() { console.log('promise3'); }); console.log('script end')
范例
function app() { setTimeout(() => { console.log("1-1"); Promise.resolve().then(() => { console.log("2-1"); }); }); console.log("1-2"); Promise.resolve().then(() => { console.log("1-3"); setTimeout(() => { console.log("3-1"); }); }); } app();