-1

I have checked and read the other posts about making an asynchronous calls which tells me to handle the promise, this post is different because I am expecting my call to have already handled that

I am trying to get the current score of a specified user. However, instead of returning the score I am getting (in the console):

enter image description here

the current score in my firebase database is 3. Here is the function I have:

function getCurrentScore() {

    var userId = firebase.auth().currentUser.uid;
    return firebase.database().ref('/users/' + userId).child('score').once('value').then(function(snapshot) {
        console.log(snapshot.val());
    });

}

what I want to do is assign the value to a variable that I can use. For example

var score = getCurrentScore();

How can I get this value and assign it to a variable in firebase?

Edit for clarification:

So, I know that this is making an asynchronous call, however I thought that .then only works once that call has completed. Is this not the case? Does firebase handle this differently?

Here is what I thought from https://howtofirebase.com/promises-for-firebase-bbb9d0d595ed:

Returning a value out of a .then() callback and returning Promise.resolve() has the same result.

I am a noob junior so please do not crucify me. Extra points for detailed explanations that help me learn and increase my understanding..

Community
  • 1
  • 1
modusTollens
  • 397
  • 7
  • 23
  • `.then()` *registers* a function to be called when the operation completes, and it also returns a Promise so that other `.then()` operations can be registered. That's a basic characteristic of Promise APIs. – Pointy Aug 21 '19 at 06:24
  • 1
    There is simply no way to directly "wait" and extract the result from the Promise. You have to install a callback, and the easiest way to do that is using `then`. You can get something that looks like sequential code using the `async/await` syntax (but it is still Promises under the hood). – Thilo Aug 21 '19 at 06:25
  • Make sure you read this answer first for a good introduction: https://stackoverflow.com/a/47043798/14955 – Thilo Aug 21 '19 at 06:27
  • The promise will be resolved and the value returned *sometime later*. There's no way to get the value immediately out of `getCurrentScore` since it isn't available *right now*. `getCurrentScore` won't wait for the promise to complete, and neither should it, as that would block everything else too. – deceze Aug 21 '19 at 06:36

1 Answers1

2

What i do usually in this type of cases is that i return the promise from the function and handle the promise directly where i need the returned data. e.g i would change it to

function getCurrentScore() {
  var userId = firebase.auth().currentUser.uid;
  return firebase.database().ref('/users/' + userId).child('score').once('value');
}

And than wherever i need the data i would do:

getCurrentScore().then(function(snapshot){
   var score = snapshot.val();
})

Or you can also work something out with async/await to(still a promise). The main point is you leave the responsibility of handling promise to the function which needs the data

Azzam Asghar
  • 346
  • 1
  • 10
  • Thank you. I literally copied and pasted the code from the docs in Firebase. Your solution is different but it works. Thanks again :) – modusTollens Aug 21 '19 at 06:36