【干货】顺序执行

方法1:脚本顺序执行

javascript 复制代码
testFun001(){
    api.commonAjax666({}).then((res)=>{
        if(res.code==200){
            console.log("第一个执行!")
            this.testFun002()
        }
    })
},
testFun002(){
    console.log("第二个执行!")
}

方法2:new Promise()

javascript 复制代码
this.testFun()

testFun(){
    this.testFun001().then((res)=>{
        console.log("第一个执行!")
        this.testFun002()
    })
},
testFun001(){
    return new Promise((resolve, reject)=>{
        api.commonAjax666({}).then((res)=>{
            if(res.code==200){
                resolve(res)
            }
        })
    })
},
testFun002(){
    console.log("第二个执行!")
}

方法3:Promise.all()

javascript 复制代码
Promise.all([
    this.testFun001()
]).then((res)=>{
    this.testFun002()
})
testFun001(){
    return new Promise((resolve, reject)=>{
        api.commonAjax666({}).then((res)=>{
            if(res.code==200){
                console.log("第一个执行!")
                resolve(res)
            }
        })
    })
},
testFun002(){
    console.log("第二个执行!")
}

方法4:Promise.race()

注意:

(1)写法同方法3,只需将 all 修改为 race

(2)区别:Promise.all() 所有数据;Promise.race() 先请求回来数据

相关推荐
love530love6 小时前
Scoop 完整迁移指南:从 C 盘到 D 盘的无缝切换
java·服务器·前端·人工智能·windows·scoop
i220818 Faiz Ul6 小时前
计算机毕业设计|基于springboot + vue鲜花商城系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
王码码20357 小时前
Flutter for OpenHarmony:Flutter 三方库 bluez 玩转 Linux 风格的蓝牙操作(蓝牙底层互操作)
linux·运维·服务器·前端·flutter·云原生·harmonyos
暴力袋鼠哥9 小时前
基于 Spring Boot 3 + Vue 3 的农产品在线销售平台设计与实现
vue.js·spring boot·后端
chilavert31810 小时前
技术演进中的开发沉思-371:final 关键字(中)
java·前端·数据库
2301_8169978810 小时前
Vite构建工具
前端
yuki_uix11 小时前
深入理解 reduce:从面试题到设计思维
前端
凌云拓界11 小时前
TypeWell全攻略(二):热力图渲染引擎,让键盘发光
前端·后端·python·计算机外设·交互·pyqt·数据可视化