张国伟 发表于 2024-8-12 13:11:32

前端 JS 经典:Promise 详解

1. Promise 由来

在以前我们实现异步是用的回调函数,当一个异步哀求需要依赖上一个异步哀求返回的结果的时候,就会形成如下这种的调用布局。
请求1(function (结果1) {
请求2(function (结果2) {
    请求3(function(结果3)) {
      请求4(function(结果4) {})
    }
});
}); 这种布局看着很紊乱且不易维护,被我们亲切的叫做 “回调地狱”。为了办理回调地狱的问题,Promise 就诞生了。
Promise 诞生的使命:优雅的去表示异步回调。
2. Promise 基础概念

2.1 创建 promise 实例

Promise 是一个构造函数,所以我们在调用一个 Promsie 的时候,要用 new Promsie 的方式去调用。
const ps = new Promise(); new Promise 接受一个函数类型的参数。这个函数又吸取两个参数 resolve 和 reject,分别对映成功回调和失败回调。Promise 内部有 3 种状态 pending(举行时),fulfilled(已成功),rejected(已失败)。且这三种状态是不可逆的,只能由 pending 到 fulfilled,pending 到 rejected。resolve 使 promise 状态由 pending 到 fulfilled,reject 使 promise 状态由 pending 到 rejected。
const ps = new Promise(function () {});  https://img-blog.csdnimg.cn/direct/4176db9dec0a424eb90ca127dfcf9146.jpeg
const ps = new Promise(function (resolve, reject) {
let number = 10;
if (number >= 10) {
    resolve(number);
} else {
    reject("number 小于10");
}
});  https://img-blog.csdnimg.cn/direct/ad8f7f1eabfc4441b29db0a23e66c210.jpeg
2.2 实例方法

为了更好的控制下一步执行,又诞生了三个实例方法 then、catch、finally。
2.2.1 then 方法

then 方法一样平常接受两个参数 resolve、reject。分别对应成功时的回调和失败时的回调。
成功状态的 promise 实例

const ps1 = new Promise(function (resolve, reject) {
let number = 10;
if (number >= 10) {
    resolve(number);
} else {
    reject("number 小于10");
}
});

ps1.then(function (res) {
console.log(res);
}); https://img-blog.csdnimg.cn/direct/2e3996a8b88847789e1498c7456ccf25.jpeg
失败状态的 promise 实例

const ps2 = new Promise(function (resolve, reject) {
let number = 9;
if (number >= 10) {
    resolve(number);
} else {
    reject("number 小于10");
}
});

ps2.then(
function (res) {},
function (err) {
    console.log(err);
}
); https://img-blog.csdnimg.cn/direct/e13e1a2bf50246ce8a1a7bff902f013c.jpeg
2.2.2 catch 方法

then 方法支持链式调用,then 的执行严峻依赖上一步的结果,上一步假如没有结果,那么下一步就不会执行。但是每一步都写 reject 特别麻烦,所以诞生了 catch,then 就可以省略 reject 方法,reject 方法就交给 catch 执行,同时 catch 还可以捕获执行 resolve 的非常。
const ps2 = new Promise(function (resolve, reject) {
let number = 9;
if (number >= 10) {
    resolve(number);
} else {
    reject("number 小于10");
}
});

ps2
.then(function (res) {})
.catch(function (err) {
    console.log(err);
}); 2.2.3 finally 方法

有的时候,无论成功还是失败都需要执行一些利用,就诞生了 finally。我们可以在这做一些清算工作。
const ps2 = new Promise(function (resolve, reject) {
let number = 9;
if (number >= 10) {
    resolve(number);
} else {
    reject("number 小于10");
}
});

ps2
.then(function (res) {})
.catch(function (err) {
    console.log(err);
})
.finally(function () {
    console.log("end");
}); https://img-blog.csdnimg.cn/direct/3bbd51e961ee4991a731901b858dc39d.jpeg
2.2.4 then 的链式调用

因为 then 方法支持链式调用,所以我们需要知道使用 then 方法会有什么结果。只要调用 promise 实例的 then 方法一定产生一个新的 promise,这个 promise 的状态由里面的函数决定。函数什么时候有结果,这个新 promise 状态什么时候由 pending 转 fulfilled。catch 和 finally 同理。
const ps = new Promise(function (resolve, reject) {
let number = 10;
if (number >= 10) {
    resolve(number);
} else {
    reject("number 小于10");
}
});ps.then(function (res) {return res * 10;}).then(function (res) {    return res + 2;}).then(function (res) {    if (res > 100) {      console.log(res);    } else {      console.log("最后结果小于100");    }}); https://img-blog.csdnimg.cn/direct/ada0f0507600458598e29a37befa7a72.jpeg
3. Promise 原型方法

为了满意更多的业务需要,又诞生了 6 个原型方法 Promise.all()、Promise.allSettled()、Promise.any()、Promise.race()、Promise.reject()、Promise.resolve()。且都会返回一个确定状态的 Promsie 对象。
统一使用两个成功异步 ps1、ps2。两个失败异步 ps3、ps4。作为测试用例。如下:
const ps1 = new Promise(function (resolve, reject) {
setTimeout(() => {
    resolve("成功结果1");
}, 1000);
});

const ps2 = new Promise(function (resolve, reject) {
setTimeout(() => {
    resolve("成功结果2");
}, 2000);
});

const ps3 = new Promise(function (resolve, reject) {
setTimeout(() => {
    reject("失败结果1");
}, 3000);
});

const ps4 = new Promise(function (resolve, reject) {
setTimeout(() => {
    reject("失败结果2");
}, 2000);
}); 3.1 Promise.all()

通过数组的形式传入异步利用,全部的异步执行都成功,才执行成功,假如有一个失败了,就执行失败。例子如下:
都成功,执行 then 方法,成功返回结果,通过 res 以数组的方式返回
Promise.all()
.then(function (res) {
    console.log("都成功", res);
})
.catch(function (err) {
    console.log(err);
}); https://img-blog.csdnimg.cn/direct/5895a842282f41cfb2d70eed958991c5.jpeg
按传入顺序执行,假如有一个异步执行失败,中断其他异步利用,执行 catch 方法,通过 err 返回失败结果
Promise.all()
.then(function (res) {
    console.log("都成功", res);
})
.catch(function (err) {
    console.log("执行失败", err);
}); https://img-blog.csdnimg.cn/direct/8f4852de357e4c64b965493838b317d3.jpeg
3.2 Promise.allSettled()

全部执行都有结果了就执行,无论成功还是失败
Promise.allSettled().then(function (res) {
console.log("都返回结果", res);
}); https://img-blog.csdnimg.cn/direct/86d0a6260884470aad55056fce154221.jpeg
3.3 Promise.any()

任意异步成功了就执行
Promise.any().then(function (res) {
console.log("任意成功", res);
}); https://img-blog.csdnimg.cn/direct/35c78c9512ba4ec2a7094e4e032bf119.jpeg
3.4 Promise.race()

任意异步成功或失败就执行。
Promise.race().then(function (res) {
console.log("任意成功或失败", res);
}); https://img-blog.csdnimg.cn/direct/84ef73b235e34e0ab88839d3624853d9.jpeg
3.5 Promise.reject()

返回一个状态为 rejected 的 Promise 对象
https://img-blog.csdnimg.cn/direct/46e1e44ef9c14c9d9bccd3dc4f03f798.jpeg
3.6 Promise.resolve()

会吸取 4 种类型的参数:promise 实例 、具有 then 方法的对象、没有 then 方法的对象、不传参
当参数是一个 Promise 实例时,直接返回这个实例。
Promise.resolve(ps1); https://img-blog.csdnimg.cn/direct/0f61ddf70c6f42d68a4e98888ffe3d56.jpeg
 当参数是一个具有 then 方法的对象时,将这个对象转为 Promise 对象,并立即执行对象的 then 方法
let obj = {
name: "yqcoder",
then(resolve) {
    resolve(this.name);
},
};

Promise.resolve(obj); https://img-blog.csdnimg.cn/direct/1bff560ed72d4044b8af46bcaf321fb7.jpeg
当参数是一个没有 then 方法的对象,大概参数不是对象时,就会返回状态为 fulfilled 的新的 Promise 对象,并将参数传入下一个 then。
let obj = {
name: "yqcoder",
};

let ps = Promise.resolve(obj);

ps.then(function (res) {
console.log("结果", res);
});  https://img-blog.csdnimg.cn/direct/caa136a2751742679921dec1ef33d8bd.jpeg
当不带任何参数时,就会返回一个状态为 fulfilled 的 Promise 对象
Promise.resolve();  https://img-blog.csdnimg.cn/direct/3c0d51c9c49f4e52acfdc8c7e3fad5fe.jpeg

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 前端 JS 经典:Promise 详解