简体   繁体   中英

How to await an array that is filled by data fetched from promises to get filled?

I currently have some promises that must fill an array, but the problem is that I cant operate with that array because it gets read before the promises finish, and im unable to log it in any way. I have read about promise.all but I dont understand how to implement it. Any idea?

    //array to be filled
  var members = []
 //function to check if an user is a member 
  function checkIfMember(uid, cid){
    return ctx.getChatMember(uid, cid).then(data =>{
        if(data.status == "member"){
            return true;
        }else{
            return false;
        }
    }).catch(err =>{
        console.log(err)
    })
  }

  //Returns all the user and the number of users
  db.query("SELECT * FROM users").then(data => {
      console.log(data)
      console.log(data.length)
      //in the returned user list we check that each member is in the group, if it is, we push the member to an array
     for(let i in data){
         checkIfMember(data[i].tg_id,chatid).then(res =>{
             if(res){
                 members.push(data[i].tg_id)

             }
         }).catch(err =>{
             console.log(err)
         })
     }
     console.log(members) //EMPTY --> how or where do i log this? !!QUESTION <<<<<------------
    }).catch(err => {
      console.log(err)
  })

You'll need to handle a promises list, and each promise (which is the checkIfMemeber ) will be pushed to that array.

Then, you can use Promise.all which will trigget then when all the promises in the list are resolved:

  //Returns all the user and the number of users
  db.query("SELECT * FROM users").then(data => {
      console.log(data)
      console.log(data.length)
      //in the returned user list we check that each member is in the group, if it is, we push the member to an array
     let promiseList = []
     for(let i in data){
         let promise = checkIfMember(data[i].tg_id,chatid).then(res =>{
             if(res){
                 members.push(data[i].tg_id)

             }
         }).catch(err =>{
             console.log(err)
         })
         promiseList .push(promise)
     }

     Promise.all(promiseList).then((res) => {
        console.log(members);
     });

    }).catch(err => {
      console.log(err)
  })

This is one of the possible solutions:

//array to be filled
var members = []
//function to check if an user is a member 
function checkIfMember(uid, cid) {
    return ctx.getChatMember(uid, cid).then(data => {
        return data.status === "member";
    }).catch(err => {
        console.log(err)
    })
}

//Returns all the user and the number of users
db.query("SELECT * FROM users").then(data => {
    console.log(data)
    console.log(data.length)
    const promises = data.map(user => {
        return checkIfMember(user.tg_id, chatid).then(isMember => {
            if (isMember) {
                members.push(user.tg_id);
            }
        })
    });

    Promise.all(promises).then(() => {
        console.log(members) 
    });
}).catch(err => {
    console.log(err)
})

But this code is still not very good.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM