美文网首页
js多个请求,需请求结束才能执行的办法

js多个请求,需请求结束才能执行的办法

作者: 甘道夫老矣 | 来源:发表于2023-12-27 17:03 被阅读0次

    场景:有些时候涉及多个请求,又必须等这些请求出来了,才能执行后面的逻辑,比如编辑回填的时候,需等待字典项

      // 获取批文二级类型
                    function getSplxEjlxs() {
                        const promises = [
                            new Promise((resolve, reject) => {
                                Service.GetList1('', function (response) {
                                    if (response.data && response.data.length > 0) {
                                        $
                                        resolve(); // 请求成功,调用 resolve 函数
                                    } else {
                                        reject('请求失败'); // 请求失败,调用 reject 函数
                                    }
                                });
                            }),
                            new Promise((resolve, reject) => {
                               Service.GetList2("TDGY", function (response) {
                                    if (response.data && response.data.length > 0) {
                                        $scope.splxejlxsTDGY = response.data;
                                        resolve(); // 请求成功,调用 resolve 函数
                                    } else {
                                        reject('请求失败'); // 请求失败,调用 reject 函数
                                    }
                                });
                            }),
                            new Promise((resolve, reject) => {
                               Service.GetList3("NYDZY", function (response) {
                                    if (response.data && response.data.length > 0) {
                                   
                                        resolve(); // 请求成功,调用 resolve 函数
                                    } else {
                                        reject('请求失败'); // 请求失败,调用 reject 函数
                                    }
                                });
                            }),
                            new Promise((resolve, reject) => {
                               Service.GetList4("XXYD", function (response) {
                                    if (response.data && response.data.length > 0) {
                                     
                                        resolve(); // 请求成功,调用 resolve 函数
                                    } else {
                                        reject('请求失败'); // 请求失败,调用 reject 函数
                                    }
                                });
                            })
                        ];
                        return Promise.all(promises);
    
                    }
    

    使用

             // 调用 getSplxEjlxs() 方法获取审批类型字典项,并在其返回的 Promise 对象上使用 then 方法执行后续操作
                            getSplxEjlxs().then(() => {
                                // 请求完成后执行的后续代码
                              //todo
                            }).catch((error) => {
                                // 请求失败时的处理逻辑
                                console.log('请求失败:', error);
                            });
    
    

    相关文章

      网友评论

          本文标题:js多个请求,需请求结束才能执行的办法

          本文链接:https://www.haomeiwen.com/subject/euorndtx.html