0

am trying path an html container from firebase,. all data should be stack according, this is my code:

firebase.database().ref('message/' + user.uid).on('value', function (snap) {

    console.log('at the first position');
    firebase.database().ref('chat/today').on('value', function (message) {
        console.log('at the second position');

        firebase.database().ref('day/tuesday').on('value', function (snap) {
            console.log('at the third position');

            firebase.database().ref('scores/' + user.uid).on('value', function (snap) {
                console.log('at the fourth position');
            });


            console.log('at the fifth position');


            firebase.database().ref('user/friends').on('value', function (snap) {
                console.log('at the six position');
            });

            console.log('at the seventh position');;
        });

        console.log('at the eight position');
    });
    console.log('at the nineth position');
});

I want to know if I use async / wait on all firebase.database() calls would it run synchronously?

I am expecting this console output:

//at the first position
//at the second position
//at the third position
//at the fourth position
//at the fifth position
//at the six position
//at the seventh position
//at the eight position
//at the nineth position

but am getting this :

//at the first position
//at the nineth position
//at the second position
//at the eight position
//at the third position
//at the fifth position
//at the seventh position
//at the fourth position
//at the six position
plr108
  • 1,201
  • 11
  • 16
Anthony
  • 53
  • 1
  • 6
  • 2
    Yes, using await will make this code no-longer nested and "synchronous". Why not try it first? https://stackoverflow.com/a/47713500/5734311 –  Oct 08 '20 at 13:11
  • You almost certainly want to use once() instead of on() to perform single queries. – Doug Stevenson Oct 08 '20 at 16:01
  • @Chris G, by using>>> const eventref = const snapshot = await eventref.once('value') ; won`t this assignment need to be wrap in an async function? – Anthony Oct 08 '20 at 17:57
  • Yes, you can use await only inside async functions. –  Oct 08 '20 at 18:13
  • thanks alots, it works but i must wrap all inner functions in one parent async function to keep the flow going – Anthony Oct 10 '20 at 09:34

0 Answers0