3

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

私はあなたが[ここ](http://stackoverflow.com/questions/14408718/wait-for-callback-before-continue-for-loop)の答えを見つけることができると思います。 –

+0

'await'関数を使用して更新が完了するのを待つことができるので、あなたのコードでES6とバベルを使用していますか? – Sylwit

+0

私はバベルでES6を使用します。 –

答えて

1

このようなものがあります。

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

これは解決済みの約束事から始まります。これにより、最初の更新がすぐに実行されます。各後続の更新は、前回の約束の解決に連鎖されます。

関連する問題