2

I'd like pull a value via firebase api and make it a global variable, so that I can refer to it else where in the code

I'd like for the below code to work where I pull a value from firebase and can use it outside of the function (http://jsfiddle.net/chrisguzman/jb4qLxtb/)

var ref = new Firebase('https://helloworldtest.firebaseIO.com/');

ref.on('value', function (snapshot) {
    var Variable = snapshot.child("text").val();
    return Variable;
});

alert(Variable);

I've tried defining it with var below with no luck (http://jsfiddle.net/chrisguzman/jb4qLxtb/1/)

var ref = new Firebase('https://helloworldtest.firebaseIO.com/');

var MyVariable = ref.on('value', function (snapshot) {
    var Variable = snapshot.child("text").val();
    return Variable;
});


alert(MyVariable);

I've also tried to define it as a function with no luck: http://jsfiddle.net/chrisguzman/jb4qLxtb/2/

var ref = new Firebase('https://helloworldtest.firebaseIO.com/');

function myFunction() { ref.on('value', function (snapshot) {
    var Variable = snapshot.child("text").val();
    return Variable;
});};


alert(myFunction());
Chris
  • 5,444
  • 16
  • 63
  • 119
  • You *don't* in this case. See http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call and http://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript – user2864740 Oct 30 '14 at 01:36

2 Answers2

1

With asynchronous programming, you can only use the response inside the callback or in a function you call from there and pass the data to. You can't stuff it into a global variable to try to work around the fact that the response is asynchronous. You also can't return a value form an asynchronous callback and expect that value to be returned from the host function. The host function has already finished executing and the callback is called some time later.

Here's one way that would work:

var ref = new Firebase('https://helloworldtest.firebaseIO.com/');

ref.on('value', function (snapshot) {
    var Variable = snapshot.child("text").val();
    alert(Variable);
});

To read a lot more about your options for handling an async response, you can read this answer which is about an ajax call, but the concept is the same: How do I return the response from an asynchronous call?.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
-1

You should declare global vars outside of $( document ).ready(function()

var MyVariable = '';
$( document ).ready(function() {
var ref = new Firebase('https://helloworldtest.firebaseIO.com/');

MyVariable = ref.on('value', function (snapshot) {
    var Variable = snapshot.child("text").val();
    return Variable;
});


alert(MyVariable);
});
Héctor
  • 509
  • 2
  • 7
  • 2
    `ref.on` is an asynchronous operation. – user2864740 Oct 30 '14 at 01:37
  • Tried here: http://jsfiddle.net/chrisguzman/jb4qLxtb/3/ . Is there a way to not make it asychronous? – Chris Oct 30 '14 at 01:37
  • @Chris No. Asynchronous is asynchronous and must be used as so. This is actually a good thing (in browser JavaScript), even though it's a bit hard to get used to. Using Promises can greatly simplify the logic: but they are just callbacks in disguise. – user2864740 Oct 30 '14 at 01:40