Make Asynchronous method Synchronous javascript -


i have javascript code pulls data out of 2 sources.

1st source local web storage

2nd source ajax request.

the condition simple :

    function getmydata(){     if (window.localstorage['mydata'] != null)     {         return window.localstorage['mydata'];     }     else     {         networktools.ajax("geymydata", function (data)         {             return data;         })     }} 

but problem ajax async process , don't want code continue until getmydata() return something.

i know can use callbacks, wait until function returns , continue execution. (not case, general knowledge.)

is possible?

use callback

    function getmydata(callback){         if (window.localstorage['mydata'] != null)         {             callback(window.localstorage['mydata'];)         }         else         {             networktools.ajax("geymydata", function (data)             {                 callback(data);             })         }   } 

how call getmydata

getmydata(function(data){       //your code }); 

or can make ajax request synchronous request.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -