7***@qq.com
7***@qq.com
  • 发布:2021-02-26 14:42
  • 更新:2021-03-10 18:07
  • 阅读:634

同步执行顺序,为什么不是前面一个执行完才执行下一个啊?

分类:uni-app
onLoad(e) {  
        console.log("cate load")  
        if(!e.dir){  
            return uni.showToast({  
                title: '获取数据失败,缺少必要参数'  
            })  
        }  
        this.current = e.p ? e.p : 1;  
        // console.log(this.current)  

        this.dir = e.dir  
        this.getCate(e.dir)  
        this.getList(59,this.current)  
    },  
    methods:{  
        async getCate(dir){  
            console.log("getCate-111111111111")  
            await this.tools.httpRequest({  
                url: "/cate.html?dir="+dir  
            }).then(res => {  
                console.log("get-cate-----res")  
                this.cate = res.data.data.cate;  
                this.cid = res.data.data.cate.catid;  
                this.navTitle = res.data.data.cate.title;  
                this.hasChild = res.data.data.cate.has_child;  
                this.tpl = res.data.data.cate.cate_tpl;  
            })  
        },  
        async getList(cid,p){  
            await this.tools.httpRequest({  
                url: "/list-page.html?cid="+cid+"&has_child="+this.hasChild+"&page="+p  
            }).then(res => {  
                console.log("get-getList-----res")  
                this.cList = res.data.data.list;  
                this.total = res.data.data.count;  
            })  
        },  
}  

const httpRequest = (options)=>{
return new Promise((resolve,reject)=>{
console.log("http-request:"+options.url)
uni.request({
url:BASE_URL+options.url,
method: options.method || 'GET',
data: options.data || {},
success: (res)=>{
console.log("http-succ:"+options.url)
// console.log(res)
if(res.data.code !== 0) {
return uni.showToast({
title: '获取数据失败'
})
}
resolve(res)
},
fail: (err)=>{
uni.showToast({
title: '请求接口失败'
})
reject(err)
}
})
})
}
上面两个方法,为什么不是先getCate执行完getCate方法之后,再执行getList方法,打印的结果是getCate还没有执行完就开始执行getList方法了

2021-02-26 14:42 负责人:无 分享
已邀请:
HZbb

HZbb

async onLoad(e) {
console.log("cate load")
if(!e.dir){
return uni.showToast({
title: '获取数据失败,缺少必要参数'
})
}
this.current = e.p ? e.p : 1;
// console.log(this.current)

    this.dir = e.dir    
    await this.getCate(e.dir)    
    await this.getList(59,this.current)    
},  
全栈工程师

全栈工程师 - 精通mui、uniapp,承接相关项目外包,解决各种疑难问题。有任何问题可以随时联系,QQ:419761282

使用顺序内嵌执行

该问题目前已经被锁定, 无法添加新回复