Call An Asynchronous Javascript Function Synchronously -
first, specific case of doing wrong way on-purpose retrofit asynchronous phone call synchronous codebase many thousands of lines long , time doesn't afford ability create changes "do right." hurts every fiber of being, reality , ideals not mesh. know sucks.
ok, out of way, how create could:
function dosomething() { var data; function callback(d) { info = d; } myasynchronouscall(param1, callback); // block here , homecoming info when callback finished homecoming data; }
the examples (or lack thereof) utilize libraries and/or compilers, both of not viable solution. need concrete illustration of how create block (e.g. not leave dosomething function until callback called) without freezing ui. if such thing possible in js.
"don't tell me how should "the right way" or whatever"
ok. but should right way... or whatever
" need concrete illustration of how create block ... without freezing ui. if such thing possible in js."
no, isn't possible block running javascript without blocking ui.
given lack of information, it's tough offer solution, 1 alternative may have calling function polling check global variable, have callback set data
global.
function dosomething() { // callback sets received info global var function callback(d) { window.data = d; } // start async myasynchronouscall(param1, callback); } // start function dosomething(); // create sure global clear window.data = null // start polling @ interval until info found @ global var intvl = setinterval(function() { if (window.data) { clearinterval(intvl); console.log(data); } }, 100);
all of assumes of course of study can modify dosomething()
. don't know if that's in cards.
if can modified, don't know why wouldn't pass callback dosomething()
called other callback, improve stop before trouble. ;)
oh, heck. gave illustration suggests can done correctly, i'm going show solution...
function dosomething( func ) { function callback(d) { func( d ); } myasynchronouscall(param1, callback); } dosomething(function(data) { console.log(data); });
because illustration includes callback passed async call, right way pass function dosomething()
invoked callback.
of course of study if that's thing callback doing, you'd pass func
directly...
myasynchronouscall(param1, func);
javascript asynchronous
No comments:
Post a Comment