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


After

The normal synchronous kind of work is good and simple.

  • A function takes turns in the inputs.
  • The function performs an action with those information.
  • Function gives one value (one value)
  • The value that can be isolated is stored in a variable, the synchronous code is used directly in other parts, etc.

But async does not seem capable of doing this.

Let's say that I have a service that I want to sell. The price of the service varies from place to place

I have a user:

  • Enter in the zip code
  • when returning to the city's name The PIN code is sent to the API.
  • I use the name of the city to run a super huge function which returns the price.
  • I want to use Price in other parts of the synchronous code; and the synchronous code are spread over a long time and it is all done value < / Strong> depending on the price.

Code:

  var CalcPrice = function (city) {// Runs the return value of the goods; }; // Async function, derived from http://www.zippopotam.us/ // "place name" city Var returnLocationInfoByZip = function (ZIP, callback) {var client = new XMLHttpRequest (); Miscellaneous feedback; Client.open ("GET", "http://api.zippopotam.us/us/" + ZIP, true); Client.onreadystatechange = function () {if (client.readyState == 4) {response = client.responseText; Callback (json.parse); }; }; Client.send (); }; Var zip = "94043"; Value of Var = Return Location (InfoByZip) (Zip, Function (Response) {calcPrice (response.places [0] ["Name of place"]);}); // this does not work. I'm going to call it "Dependent Processing" section of my code. FunctionThatDependsOnPrice (price); AnotherFunctionThatDependsOnPrice (price); MoreFunctionsThatDependsOnPrice (price); EvenMoreFunctionsThatDependOnPrice (price); // I rather ReturnLocationInfoByZip (ZIP, function (response) work will find that {value = calcPrice (response.places [0] [ "place name"]); FunctionThatDependsOnPrice (price); AnotherFunctionThatDependsOnPrice (price); MoreFunctionsThatDependsOnPrice ( Price); Even more forum values, each value (value);});  

Carrying all of that code into the callback.

I just want to use the value in the inside of the variable is very ugly and confusing my normal synchronous code, but calcPrice value not Returns and the price is not stored in the variable calcPrice is always forces me to asynchronous branch / callback in processing all my other up my code Asynchronous Shows of Flow Got trapped inside the cakha.

Then the value of some question:

  • Is it true that the asynchronous code is not able to return a value back to a synchronous code?

  • Is a fat callback too much normal?

I can go on the promise path, but if I do so, then I am filling all my codes in my then function I'm ... it looks a bit clearer, but it's still deep inside.

is not able to return a value in the correct asynchronous code synchronous code ?

Yes, very much it depends on the environment though. For example: a file in the browser depends on the playlist library such as freezes asynchronous flux. Although in NodJess, they tabulated it so that you can get dependency synchronous style (like with import in Java).

If you need to bring a resource from another webservice or database, you will always need a callback because of the asynchronous nature of this JavaScript engine. Whenever it requests resources from the underlying environment (for example the browser), the script execution will not stop. Just as you can simultaneously perform many asynchronous requests together.

Is a fat callback quite normal?

It depends on your preference and code style. If it is readable and maintainable, why should not you have a fat callback? Many argue that although not clear privately, I prefer to prefer a more flat architecture by applying something like this.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -