【干货】顺序执行

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

相关推荐
newxtc30 分钟前
【爱给网-注册安全分析报告-无验证方式导致安全隐患】
前端·chrome·windows·安全·媒体
一个很帅的帅哥1 小时前
axios(基于Promise的HTTP客户端) 与 `async` 和 `await` 结合使用
javascript·网络·网络协议·http·async·promise·await
刘志辉1 小时前
vue传参方法
android·vue.js·flutter
dream_ready1 小时前
linux安装nginx+前端部署vue项目(实际测试react项目也可以)
前端·javascript·vue.js·nginx·react·html5
编写美好前程1 小时前
ruoyi-vue若依前端是如何防止接口重复请求
前端·javascript·vue.js
flytam1 小时前
ES5 在 Web 上的现状
前端·javascript
喵喵酱仔__1 小时前
阻止冒泡事件
前端·javascript·vue.js
GISer_Jing2 小时前
前端面试CSS常见题目
前端·css·面试
某公司摸鱼前端2 小时前
如何关闭前端Chrome的debugger反调试
javascript·chrome
八了个戒2 小时前
【TypeScript入坑】什么是TypeScript?
开发语言·前端·javascript·面试·typescript