Asynchronous poisoning - I can't be the only one suffering

Remember "const poisoning" in C ++, when you marked one method as const , and then you realized that you need to mark all the methods that he called const , and then all the methods that they called, and so on?

I have a problem with the reflection of asynchrony in Javascript, although I do not think this is relevant, although it is distributed, not down. If a function can call an asynchronous function, it itself must be rewritten as asynchronous, and then all the functions that call it must be, etc.

I do not have a clearly formulated question (sorry, mods), but I was hoping that someone would have (a) advice or (b) a link that might have (a).

+6
source share
3 answers

The best solution I've seen so far is promises . All that happens, of course, is that you trade in asynchronous poisoning to promise poisoning (since any calculation that depends on the promise itself should return the promise, but promises are much more flexible and powerful than callbacks.

+1
source

This is not a bad question. However, there are several ways to completely destroy your control. Note. I did not say that it was beautiful.

Suppose you have objects A, B, C, and D, A.Amethod nothing and calls the getBData method from B, B calls the getCData method for C, and problem C causes D something like this

 var data = D.getRawData(); ... something to be done with data ... ... something else to be done with data... 

and now it should be written as

 D.getData(function(data){ ... something to be done with data ... ... something else to be done with data... }); 

well, you can always add a callback parameter for each of your methods so that the code uses the code that looks like this:

  var A = { //I'm not recommending coding like this, just for demonstration purposes. ... Amethod: function(x,y,z){ var somethingForA = this.B.getBData(1,2,3); astatement1; astatement2; ... } ... } //end of A ... var B = { ... Bmethod: function(x,y,z){ var somethingForB = this.C.getCData(1,2,3); bstatement1; var somethingFromB = bstatement2; return somethingFromB; } ... } //end of B ... var C = { ... Cmethod: function(x,y,z){ var somethingForC = this.D.getRawData(1,2,3) cstatement1; var somethingFromC = cstatement2; return somethingFromC; } ... } //end of C ... 

Now you will have:

  var A = { ... Amethod: function(x,y,z){ this.B.getBData((1,2,3,function(somethingForA){ astatement1; astatement2; }); ... } ... } //end of A ... var B = { ... Bmethod: function(x,y,z,callback){ this.C.getCData(1,2,3,function(somethingForB){ bstatement1; var somethingFromB = bstatement2; callback(somethingFromB); }); ... } ... } //end of B ... var C = { ... Cmethod: function(x,y,z,callback){ this.D.getRawData(1,2,3,function(somethingForC) { cstatement1; var somethingFromC = cstatement2; callback(somethingFromC); }); } ... } //end of C ... 

This is a fairly simple refactoring using anonymous functions to implement all the functions that support the control flow. I do not know how much this would be a literal transformation; you may have to do some tuning for the scope variable. And obviously this is not so pretty.

Are there any other ways? Of course. As you can see, the above is useless, and we hope that we will not write dirty code. How less randomly depends on the context.

You do not need to pass a callback parameter, any necessary callback can be passed to objects in advance or can be passed as one of the elements in the parameter. The callback may not necessarily have to be called directly, but indirectly called using one of the available event handling methods (which you will need to see which libraries you can use to do this), and the data can be transferred when the event fires. Or maybe there is a global "DataGetter" that can register a callback whenever the data is "received" to completely avoid the intermediaries B and C.

Finally, there is the consideration that if you are deep in the bones, you will find that you need something that can be obtained asynchronously, and that the data is transmitted in a chain, you could do something a little back in terms of which objects should control the flow program logic (I'm honestly a dead end on how to describe why I seem to think this scenario is problematic, though.). I am inclined to think that since instances of A must contain instances of B, B, contain instances of C, etc., Instances creating auxiliary instances as part of its composition should have some degree of control as to how the sub-instances themselves are filled, instead of letting the sub-instances completely solve .... if that makes sense :-(

At this moment, I feel like I'm a little confused ... here, to hope that someone will explain the problems better than me!

+2
source

If the problem in b () should block until completion, but calls the asynchronous function a (), then perhaps the callback from () can set the flag and b () for the flag. If a () does not offer a callback, then perhaps there is a value somewhere that changes after the completion of a ().

0
source

Source: https://habr.com/ru/post/912551/


All Articles