【干货】顺序执行

方法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() 先请求回来数据

相关推荐
ChoSeitaku20 小时前
NO.2|proto3语法|消息类型|通讯录|文件读取|enum类型
java·服务器·前端
小J听不清20 小时前
CSS 边框(border)全解析:样式 / 宽度 / 颜色 / 方向取值
前端·javascript·css·html·css3
用户2557788508120 小时前
axios全局重复请求取消
前端
前端付豪20 小时前
实现一个用户可以有多个会话
前端·后端·llm
林古20 小时前
我在 WSL 里控制 Windows Chrome 的一次实战复盘(OpenClaw)
前端
想不到一个好的ID21 小时前
Claude Code 初学者必看指南
前端·后端
用户3365663421721 小时前
Vue3+Vite项目极致性能优化:从构建到运行全链路实战指南
vue.js
一枚菜鸟_21 小时前
04-Flutter状态管理终极指南-Riverpod3.x从入门到精通
前端
一枚菜鸟_21 小时前
06-Flutter动画从零到炫酷-让你的App动起来
前端
Wect21 小时前
React Hooks 核心原理
前端·算法·typescript