async/await学习
async用法1234async fn() {}const fn = async () => {}const fn = async function() {}let obj = { async fn() {} }
Promise学习(2)
Promise.all()12345678910111213function setTime(time = 0, isSuccess = true) { return new Promise((res, rej) => { setTimeout(() => { console.log(666); isSuccess && res(`成功:${time}`); !isSuccess && rej(`失败:${time}`); }, time); });}Promise.all([setTime(100), setTime(200), setTime(300)]) .then(res => console.log(res)) // (3) ["成功:100", "成功:200", "成功:300"] .catch(err...
Promise学习
Promise的状态Promise有三个状态: pending:[待定]初始状态 fulfilled:[实现]操作成功 rejected:[被否决]操作失败 当promise状态发生改变,就会触发then()里的响应函数处理后续步骤。promise状态一经改变,不会再变。 Promise对象的状态改变,只有两种可能: 从pending变为fulfilled。 从pending变为rejected。 这两种情况只要发生,状态就凝固了,不会再变了。 基本使用知识点123456789101112131415161718let p = new Promise((res, rej) => {let t = new Date(), s = t.getSeconds();if(s % 2) { res(s);}else{ rej(s, "参数二");}});# 调用方式1p.then(res => console.log(res, "成功")) ...