收藏
回答

利用promise该如何实现同步?

想实现同步,getData调用getathead,getathead又调用gethead,但出现promise未定义的报错,请问正确该怎么写

onLoad: function (options) {
        this.getData();
    },

async getData(){
        var that= this
        var id = wx.getStorageSync('info').id
        db.collection('Notice')
        .where({
            message:"@",
            noticed:id
        }).get()
        .then(res=>{
            that.setData({
                atlist:res.data
            })
            that.getathead();
        })
    },

async getathead(){
        var p = this.data.atlist;
        return new promise((reslove,reject)=>{
            for(let i =0;i<p.length;i++)
            {
                this.gethead(p.uid);
            }
            reslove()
        })
    },

gethead(id){
        var list=this.data.headlist;
        return new promise((reslove,reject)=>{
            db.collection('Student')
            .where({
                _id:id
            }).get()
            .then(res=>{
                if(res.data.length==0)
                {
                    db.collection('Teacher')
                    .where({
                        _id:id
                    }).get()
                    .then(res=>{
                        list.push(res.data[0].pic)
                        this.setData({
                            headlist:list,
                        })
                        reslove()
                    })
                }
                else
                {
                    list.push(res.data[0].pic)
                    this.setData({
                        headlist:list,
                    })
                    reslove()
                }
            })
            
        })
    },

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

1 个回答

  • 武曲心
    武曲心
    2022-05-10

    promise是首字母大写

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