0

I have a callback function and i want it to retun the value and assign it to my variable "value"

function a(callback){
     // let elecfirebase=[]
      firebase
          .database()
          .ref("/fournisseurs")
          .child(0)
          .child("data")
          .on("value", data => {
           let  elecfirebase=data.val(); 
            callback(elecfirebase);
        });

  }

let value= a( function(elecfirebase){
    return (elecfirebase)}
  );

console.log(value)

loveALgo
  • 13
  • 4
  • 1
    `value` gets set to the return value of `a`, which itself doesn't return anything. See the link above to manage this – Nick Parsons Jan 29 '20 at 04:44
  • i read this post, but i don't have the solution for a() returning value! – loveALgo Jan 29 '20 at 05:18
  • You can use a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) like [this](https://jsfiddle.net/490az8xm/2/) – Nick Parsons Jan 29 '20 at 05:22
  • Thanks you very much, it's good when i use console.log When i do `let val=a.then(function(value) {return(value); // returning value});` i have this output `Promise {} __proto__: Promise [[PromiseStatus]]: "resolved" [[PromiseValue]]: Array(4)` how can i access to [[PromiseValue]] ? – loveALgo Jan 29 '20 at 06:08
  • You can only access the value within the callback of .then, you can’t access outside. You’ll need to do your logic inside of the .then callback, or, if you use your original code, you’ll have to do your logic with value inside the function you pass to `a` – Nick Parsons Jan 29 '20 at 06:12

0 Answers0