收藏
回答

求教如何把这个for循环变成同步?

尝试了用promise,但输出编号发现还是异步执行   

 for(let i =0;i<res.data.length;i++)
   {
    var id = res.data[i].uid;
    new Promise((resolve, reject) => {
        console.log(id)
        db.collection('Student')
        .where({
            _id:id
        }).get()
        .then(res=>{
            if(res.data.length==0)
            {
                db.collection('Teacher')
                .where({
                    _id:id
                }).get()
                .then(res=>{
                    that.data.headlist.push(res.data[0].pic)
                    that.setData({
                        headlist:that.data.headlist,
                    })
                    setTimeout(function () {
                        console.log(i)
                        resolve()
                    }, 500)
                    
                }).catch(res=>{
                    console.log(res)
                })
            }
            else
            {
                that.data.headlist.push(res.data[0].pic)
                that.setData({
                    headlist:that.data.headlist,
                })
                setTimeout(function () {
                    console.log(i)
                    resolve()
                }, 500)
            }
        })
    }) 
 }

回答关注问题邀请回答
收藏

3 个回答

  • lie
    lie
    2022-06-04

    async await

    2022-06-04
    有用
    回复
  • 跨商通
    跨商通
    2022-05-18
    let ids = res.data.map(v=>v.uid)//获得一个student id 的数组
    db.collection('student').where({
      _id:_.in(ids) //查询_id包含在ids的所以记录
    }).get().then()
    
    2022-05-18
    有用
    回复
  • Frank
    Frank
    2022-05-18

    接口都自己写了,从服务端一把返回呗

    2022-05-18
    有用
    回复
登录 后发表内容