javascript can't assign variable values due to asynchronous function. -


in javascript code below, cannot user_likes take value of response. can console output "response", , has values need. however, when try console output user_likes, undefined. gives?

function fqluserlikes(user_id) {     var user_likes;     fb.api(         {             method: 'fql.query',             query: 'select page_id,name,type page page_id in (select page_id page_fan uid=' + user_id + ')'         },         function(response) {             user_likes=response;         }     );     console.log(user_likes);     return user_likes; } 

thanks help, i.n.

your method asynchronous, when try log , return variable, hasn't been assigned yet. take below snippet:

//do need in here! function(response) {     user_likes=response; });  //the below code executed before callback above. //also, should console, , not console. js case sensi. console.log(user_likes); return user_likes; 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -