微任务,宏任务,同步
传递到 then() 中的函数被置入到一个微任务队列中,而不是立即执行
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
wait().then(()=>console.log(4))
Promise.resolve().then(() => console.log(2)).then(() => console.log(3));
console.log(1);
// 输出 1 2 3 4
关于catch
catch(()=>1)是then(null, ()=>1)的简写
任何不是 throw 的终止都会创建一个"已决议"状态,而以 throw 终止则会创建一个"已拒绝"状态。
注意理解下任何:即使在被catch之后,只要catch里没有throw,catch后面的then还是被执行
Promise.reject().then(null, console.log('catch'))
//输出 catch
Promise.resolve().then(()=>{throw 2;}).catch(()=>{console.log('throw_触发“已拒绝”')}).then(()=>console.log(1))
// 输出 throw_触发“已拒绝” 1
return & params
前一个then的return值作为下一个then的参数
Promise.resolve().then(()=> 1).then(v=>console.log(v))
// 输出 1