【干货】顺序执行

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

相关推荐
盏茶作酒29几秒前
打造自己的组件库(二)CSS工程化方案
前端
mwq301232 分钟前
AI编程工具:Claude Code安装和使用
前端·claude
gnip17 分钟前
项目开发流程之技术调用流程
前端·javascript
答案—answer18 分钟前
three.js编辑器2.0版本
javascript·three.js·three.js 编辑器·three.js性能优化·three.js模型编辑·three.js 粒子特效·three.js加载模型
转转技术团队31 分钟前
多代理混战?用 PAC(Proxy Auto-Config) 优雅切换代理场景
前端·后端·面试
南囝coding33 分钟前
这几个 Vibe Coding 经验,真的建议学!
前端·后端
gnip1 小时前
SSE技术介绍
前端·javascript
yinke小琪1 小时前
JavaScript DOM节点操作(增删改)常用方法
前端·javascript
枣把儿1 小时前
Vercel 收购 NuxtLabs!Nuxt UI Pro 即将免费!
前端·vue.js·nuxt.js
望获linux1 小时前
【Linux基础知识系列】第四十三篇 - 基础正则表达式与 grep/sed
linux·运维·服务器·开发语言·前端·操作系统·嵌入式软件