【干货】顺序执行

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

相关推荐
爱生活的苏苏20 分钟前
vue生成二维码图片+文字说明
前端·vue.js
拉不动的猪22 分钟前
安卓和ios小程序开发中的兼容性问题举例
前端·javascript·面试
炫彩@之星28 分钟前
Chrome书签的导出与导入:步骤图
前端·chrome
贩卖纯净水.39 分钟前
浏览器兼容-polyfill-本地服务-优化
开发语言·前端·javascript
前端百草阁1 小时前
从npm库 Vue 组件到独立SDK:打包与 CDN 引入的最佳实践
前端·vue.js·npm
夏日米米茶1 小时前
Windows系统下npm报错node-gyp configure got “gyp ERR“解决方法
前端·windows·npm
且白1 小时前
vsCode使用本地低版本node启动配置文件
前端·vue.js·vscode·编辑器
程序研1 小时前
一、ES6-let声明变量【解刨分析最详细】
前端·javascript·es6
疯狂的沙粒2 小时前
在uni-app中如何从Options API迁移到Composition API?
javascript·vue.js·uni-app
siwangqishiq22 小时前
Vulkan Tutorial 教程翻译(四) 绘制三角形 2.2 呈现
前端