Javascript function returns undefined in other function -


i have function , want call in other function api key. if return value undefined.

how can solve this?

function getapikey(callback) {      var db = app.db;      db.transaction(         function (tx) {             tx.executesql("select api_key settings id='1'", [], function (tx, result) {                 var apikey = result.rows.item(0).api_key;                  alert(apikey); // here works                  return apikey;              });         }     ); }  function getdata() {      var mykey = getapikey();      alert(mykey); // undefined  } 

you have callback being passed param, use it! can't return async calls!

function getapikey(callback) {     var db = app.db;     db.transaction(function (tx) {         tx.executesql("select api_key settings id='1'", [], function (tx, result) {             var apikey = result.rows.item(0).api_key;             callback(apikey);         });     }); }  function getdata() {     getapikey(function(key) {         var mykey = key;          /* logic mykey should done in block */     }); } 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -