I'm working on a simple website just so I can learn FireBase and Javascript. I ran into a problem for when I want to retrieve a specific photo.
 function load(){
 let ext = null;
    var postRef = firebase.database().ref('posts/' + viewNumber +'/fileType');
    postRef.on('value', function(snapshot) {
    var value = snapshot.val();
  if (value === null) {
    return;
  }
  ext = value;
});
    console.log(viewNumber);
    console.log(ext);
    var storage = firebase.storage();
    var pathReference = storage.ref('posts/'+ viewNumber +'.'+ext);
    pathReference.getDownloadURL().then(function(url) {
    // Or inserted into an <img> element:
      var img = document.getElementById('post');
      img.src = url;
      });
  // A full list of error codes is available at
  // https://firebase.google.com/docs/storage/web/handle-errors
  };
for some reason, the first time I run the function, ext = null but the second time, it is the correct image type. When I change viewNumber and call load() again, it does the same thing, going back to null then second time to the correct extension. I was thinking I could run the function twice every time but I know tha's not proper. Anyone have any suggestions?
 
    