1

I am trying to migrate an old Javascript application to use AsyncJAX (AJAX) instead of Synchronous calls to AJAX(SJAX). I have considered few approaches, each taking quite some time

1) using async/await in order to get this work i would have to prepend every function that results in calling the SJAX somewhere in the stack by "async" and every call to SJAX by "await"

2) Analyze all possible stacktraces (i do not know of any tool that can do it for JS) and do the re-write by hand using callbacks/Promisses (don't really care about cleanliness of the code right now. There is more 200 occurences of the SJAX calls in the app so i expect there might be much more possible stacktraces to handle

3) throwing an error before the actual data-fetch (SJAX call), recording the caller somewhere and repeating the call when the data is readily available (there might be other issues, imagine running the same code twice using a "stateful" variable)

4) something completely different

My question is, what approach would you think would be the best. I know this is opinion based, but maybe there is other way to handle this elegantly (point 4). If you know an answer to that ... shoot it.

BennyHilarious
  • 373
  • 3
  • 15
  • 1
    1 won't work. `async` and `await` are tools to **manage promises**, if you don't have promises to manage they are a waste of time. – Quentin Nov 20 '19 at 12:27
  • I'm really curious about how they're making SJAX calls to begin with. What is that? I didn't know javascript was capable of making requests synchronously. Does the whole app freeze while the request is still pending? – TKoL Nov 20 '19 at 12:39
  • 1
    @TKoL: You do it by using `true` as the third parameter of `XMLHttpRequest` method [`open`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/open). `jQuery.ajax` has a similar option that it passes along to XHR. Yes, it does exactly what you think it does if you do it outside Web Worker: freezes everything. – Amadan Nov 20 '19 at 12:52
  • @Benny you're going to have to go through every SJAX call and make it async itself first, by removing whatever options or parameters in your system make it synchronous. Then you're going to have to go into every function that makes an ajax call and make them async, and every function that calls them and make them async, and so on and so forth. All the way up. Godspeed – TKoL Nov 20 '19 at 13:00
  • @TKoL ...exactly, that is what is written under point 2) .... the most tedious and most time consuming work of them all :)i'm still hoping there might be other way – BennyHilarious Nov 20 '19 at 13:05
  • 1
    The job sounds like it actually would benefit from a full rewrite from scratch tbh. If you have a good mental model of how the whole app functions and how the pieces work together, is that an option? – TKoL Nov 20 '19 at 13:08
  • @Quentin, you sure? This looks like there might be Promises internally, but rewriting the code like that should be possible https://codepen.io/dsheiko/pen/gaeqRO – BennyHilarious Nov 20 '19 at 13:09
  • @TKoL .. 500+ JS object types, doing a "big-bang" is not managebale imo. The only way would be start writing another application and fradully offsetting the functionality from the old one to the new one... – BennyHilarious Nov 20 '19 at 13:12
  • @Amadan by the way, usync sync is blocking the main event loop, it is different from running an endless loop though (by running an endless loop the system resources are exhausted, so e.g. mouse pointer becomes less responsive etc.) – BennyHilarious Nov 21 '19 at 10:47

0 Answers0