asynchronous - Want some clarification on async javascript code versus sync code -



asynchronous - Want some clarification on async javascript code versus sync code -

the normal synchronous way of doing things nice , straightforward.

a function takes in inputs. the function performs action inputs. the function ejects out value (returns value). the value ejected can stored variable, used straight in other parts of synchronous code, etc.

but async doesn't seem able this.

let's have service want sell. cost of service varies location.

i have user:

enter in zip code zip code gets sent api while returns city name. i utilize city name run super huge function returns price. i want utilize price in other parts of synchronous code, , synchronous code super long , spans across numerous functions depend on price value.

code:

var calcprice = function(city){ // stuff runs homecoming price; }; // async function, taken http://www.zippopotam.us/ // "place name" city. var returnlocationinfobyzip = function(zip, callback){ var client = new xmlhttprequest(); var response; client.open("get", "http://api.zippopotam.us/us/" + zip, true); client.onreadystatechange = function() { if(client.readystate == 4) { response = client.responsetext; callback(json.parse(response)); }; }; client.send(); }; var zip = "94043"; var cost = returnlocationinfobyzip(zip, function(response){ calcprice(response.places[0]["place name"]); }); // not work. i'm going phone call "dependent processing" part of code. functionthatdependsonprice(price); anotherfunctionthatdependsonprice(price); morefunctionsthatdependsonprice(price); evenmorefunctionsthatdependonprice(price); // think work instead returnlocationinfobyzip(zip, function(response){ cost = calcprice( response.places[0]["place name"] ); functionthatdependsonprice(price); anotherfunctionthatdependsonprice(price); morefunctionsthatdependsonprice(price); evenmorefunctionsthatdependonprice(price); });

stuffing of code in callback really ugly , confusing.

i utilize price variable within of normal synchronous code. value calcprice never gets returned , never gets stored price variable. value of calcprice forever stuck within asynchronous branch of code flow forces me other dependent processing within asynchronous branch / callback.

so few questions:

is right asynchronous code never able homecoming value synchronous code? normal synchronous way of doing , thinking things won't work.

is fat callback pretty much normal?

i go promise route, if i'm stuffing of dependent processing then function... it's little cleaner looking it's still nested deep within other things.

is right asynchronous code never able homecoming value synchronous code?

yes, pretty much. depend on enviroment though. example: in browser file depency library requirejs depends on asynchronous flow. in nodejs however, abstracted can fetch dependencies synchronous style (like import in java).

if need fetch resource webservice or database need callback. because of asynchronous nature of javascript engine. script execution not halt whenever requests resources underlying enviroment (for illustration browser). such can execute multiple asynchronous requests simultaneously.

is fat callback pretty much normal?

it depends on preference , code style. if it's readable , maintainable, why shouldn't have fat callback? many argue not clean though. personally, i'd prefer more flat architecture implementing promises.

javascript asynchronous

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -