繁体   English   中英

如何等到更新完成后才能转到下一个项目?

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

我希望它等到.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 */);
    });
  } 
}

这从一个已解决的promise开始,这将导致第一次更新立即执行。 每个后续更新都链接到先前承诺的解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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