Javascript 如何等到更新完成后再移动到下一项?

Javascript 如何等到更新完成后再移动到下一项?,javascript,asynchronous,firebase,firebase-realtime-database,generator,Javascript,Asynchronous,Firebase,Firebase Realtime Database,Generator,我希望它在转到otherPlayers数组中的下一个播放器之前等待.update()完成。我不知道发电机或其他东西是否能在这里工作 let {players} = this.props; for(let player of otherPlayers) { const index = players.findIndex(p => p.Id === player.Id); const firebaseId = index && players[index].fireb

我希望它在转到otherPlayers数组中的下一个播放器之前等待.update()完成。我不知道发电机或其他东西是否能在这里工作

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 */);
    });
  } 
}

这从一个已解决的承诺开始,这将导致第一次更新立即执行。每个后续更新都与上一个承诺的解决方案相关联。

我认为您可以在中找到答案。您是否在代码中使用ES6和babel,因为您可以使用
wait
函数使更新等待完成。我确实使用ES6和babel。