以下のようなコードがあります。
thenの中で条件分岐があり、条件に応じて次に実行するPromiseが変わります。
Promise.allについて読んでみましたが、それが使えるのか/どう使えばいいのかわからず、
ネストになってしまいました。
Javascript
1ref.once('value') 2.then((snapshot) => { 3 if (!snapshot.child('done').exists()) { 4 throw 'notOnBreak'; 5 } else { 6 var r = snapshot.child('row-index').val(); 7 var c = snapshot.child('cell-index').val(); 8 var url = snapshot.child('photo-url').val(); 9 var promise; 10 // DB: add child 11 if (url == null) { // uses camera 12 promise = roomRef.child(peerId).set({'row-index': r, 'cell-index': c}) 13 .then(() => { 14 // SW: replace stream 15 sendStream(room, peerId); 16 // set style 17 mediaSetup(room, peerId); 18 setStyleOnJoin(peerId, 'yes'); 19 }); 20 } else { // no camera 21 promise = roomRef.child(peerId).set({'row-index': r, 'cell-index': c, 'photo-url': url}) 22 .then(() => { 23 // set style 24 setStyleOnJoin(peerId, 'no'); 25 }) 26 } 27 return promise; 28 } 29}).then(() => { 30 ref.remove(); 31})
このネストを回避する方法があれば教えてください。
よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/17 18:09
2018/04/17 21:28
2018/04/17 21:34
2018/04/18 02:38
2018/04/18 02:40
2018/04/18 02:47