这怎么能等到更新完成后再移动到下一个项目呢?

How can this wait until the update is finished to move to the next item?

本文关键字:下一个 移动 项目 怎么能 更新      更新时间:2023-09-26

我想让它等到。update()完成后再去otherPlayers数组中的下一个球员。我不知道发电机或其他什么东西能不能在这里工作。

let {players} = this.props;
for(let player of otherPlayers) {
  const index = players.findIndex(p => p.Id === player.Id);
  const firebaseId = index && players[index].firebaseId;
  if(index !== -1 && firebaseId !== null) {
    window.firebase.database().ref(`/players/${firebaseId}`)
     .update({...player}, /* Callback possible */);
  } 
}

可能是这样的。

let {players} = this.props;
let p = Promise.resolve(); // just really want a promise to start
for(let player of otherPlayers) {
  const index = players.findIndex(p => p.Id === player.Id);
  const firebaseId = index && players[index].firebaseId;
  if(index !== -1 && firebaseId !== null) {
    p = p.then(()=>{
      return window.firebase.database().ref(`/players/${firebaseId}`)
       .update({...player}, /* Callback possible */);
    });
  } 
}

这从一个已解决的承诺开始,这将导致第一个更新立即执行。