实践[JavaScript] Promise.allSettled 函数的实现与应用

Promise.allSettled 函数的作用是等待一组promise,并在所有等待的promise处理完成后,返回相对应的结果,结果以数组的形式传递给下一节点。

我们在开发项目时遇到一个需求,那就是多表单校验,并需要对每个报错的表单进行一些操作,同时在所有的表单处理完成后,将通过校验的表单上传给后台。

为了解决这个需求,我们实现了两种方案,一种是对每个表单的validate进行封装,并通过withResolvers返回reject自己手动处理每个异常,外部使用Primise.all接收。而第二种就是通过Promise.allSettled来实现多表单一部校验同步等待功能。这两种方式所实现的效果是一样的。但如果关注结果顺序,则二者返回的数据则会有些许区别

使用Promise.allSettled实现

ts 复制代码
/**
 * 随机,对2取余,返回true,否则返回false
 * @returns 
 */
function validate(index: number) {
    const flag = parseInt((Math.random() * 10).toFixed(0)) % 2
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            flag ? (resolve({ index, flag })) : reject({ index, flag })
        }, Math.random() * 200)
    })
}
function saveData(res: any) {
    return Promise.resolve()
}
(async () => {
    console.log('tset promise allsettled')
    const res = await Promise.allSettled([validate(1), validate(2), validate(3), validate(4), validate(5)])
    // 获取通过校验的表单
    const list = res.filter(item => item.status === 'fulfilled')
    // 保存操作
    await saveData(list)
    console.log(res)
    /**
     * [
    *    { status: 'fulfilled', value: { index: 1, flag: 1 } },
    *    { status: 'rejected', reason: { index: 2, flag: 0 } },
    *    { status: 'rejected', reason: { index: 3, flag: 0 } },
    *    { status: 'fulfilled', value: { index: 4, flag: 1 } },
    *    { status: 'rejected', reason: { index: 5, flag: 0 } }
    *  ]
     */

})()

我们基于Promise.all封装的结果如下所示

ts 复制代码
/**
 * 随机,对2取余,返回true,否则返回false
 * @returns 
 */
function validate(index: number) {
    const flag = parseInt((Math.random() * 10).toFixed(0)) % 2
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            flag ? (resolve({ index, flag })) : reject({ index, flag })
        }, Math.random() * 200)
    })
}
function saveData(res: any) {
    return Promise.resolve()
}
function allSettled(args: Promise<any>[]) {
    if (!Array.isArray(args)) {
        throw Error("非数组")
    }
    const callList: Promise<any>[] = []
    for (let index in args) {
        function func() {
            return new Promise((resolve) => {
                const call = args[index]
                call.then((res) => {
                    resolve({ status: 'fulfilled', value: res })
                }).catch((res) => {
                    resolve({ status: 'reject', value: res })
                })
            })
        }
        callList.push(func())
    }
    return Promise.all(callList)
}
(async () => {
    console.log('tset promise allsettled')
    const res = await allSettled([validate(1), validate(2), validate(3), validate(4), validate(5)])
    // 获取通过校验的表单
    const list = res.filter(item => item.status === 'fulfilled')
    // 保存操作
    await saveData(list)
    console.log(res)
    /**
     * [
     *  { status: 'fulfilled', value: { index: 1, flag: 1 } },
     *  { status: 'reject', value: { index: 2, flag: 0 } },
     *  { status: 'reject', value: { index: 3, flag: 0 } },
     *  { status: 'fulfilled', value: { index: 4, flag: 1 } },
     *  { status: 'fulfilled', value: { index: 5, flag: 1 } }
     * ]
     */

})()

核心代码: 该函数是通过包裹一层Promise并在异常时捕获异常,并通过resolve返回,最后通过all实现join,等待所有promise全部执行

ts 复制代码
function allSettled(args: Promise<any>[]) {
    if (!Array.isArray(args)) {
        throw Error("非数组")
    }
    const callList: Promise<any>[] = []
    for (let index in args) {
        function func() {
            return new Promise((resolve) => {
                const call = args[index]
                call.then((res) => {
                    resolve({ status: 'fulfilled', value: res })
                }).catch((res) => {
                    resolve({ status: 'reject', value: res })
                })
            })
        }
        callList.push(func())
    }
    return Promise.all(callList)
}
相关推荐
QT 小鲜肉13 分钟前
【Linux命令大全】001.文件管理之mc命令(实操篇)
linux·运维·服务器·前端·笔记
土豆_potato20 分钟前
AI深度思考到底开不开
前端·aigc
ohyeah20 分钟前
React 中的跨层级通信:使用 Context 实现主题切换功能
前端·react.js
winfredzhang1 小时前
打造专属桌面时钟:纯HTML实现的全功能动态时钟
前端·html·农历·生肖·周次
哥本哈士奇1 小时前
使用Gradio构建AI前端 - RAG的QA模块
前端·人工智能·状态模式
扶我起来还能学_1 小时前
Vue3 proxy 数据响应式的简单实现
前端·javascript·vue
Dragon Wu1 小时前
前端项目架构 项目格式化规范篇
前端·javascript·react.js·前端框架
QQ 31316378901 小时前
文华财经软件指标公式期货买卖信号提示软件
java·前端·javascript
老华带你飞1 小时前
房屋租赁管理系统|基于java+ vue房屋租赁管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
惜晨宝贝2 小时前
文件上传格式限制
前端·html5·上传测试