Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
652 views
in Technique[技术] by (71.8m points)

firebase - javascript assigning value to variable from callback return (timing)

I'm having a trouble with assigning return value from Firebase call function to my global variable. This is my function:

  function getThePushNameById( path , id ){
    path.once( 'value', function( data ){ 
        if( data.child('id').val() != id ){
            data.forEach( function( newData ){
                var base = new Firebase( path.child( newData.name() ).toString() );
                getThePushNameById( base, id );
            })
        }else{
            //finishes the function when path is founded - returns the value
            return path.name();
        }       
    })
}

and this is the other file where I set the request:

var base = new Firebase(path/to/my/base);

var output = getThePushNameById( base , 2 )
console.log(output);

So my problem is that console.log doesn't wait for output to be defined, but runs itself and logs undefined. And my question is if someone knows how can I make console.log wait for value?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...