0

I currently have a few basic question with asynchronous calls in JS.

I have searched and found a similar question posted in the past here:

Parallel Ajax Calls in Javascript/jQuery

But I still have some doubts about asynchronous calls.

  1. Let´s say that I make it asynchronous using a jQuery when to join both flows when both AJAX calls are finished. What would happen if one of the AJAX calls fail?

  2. Some of the calls that I need to make should be asynchronous only in some cases. Is it a good practice to make the type of call (asynchronous true or false) variable? Or doesn't it really matter to have an asynchronous true call when there is only one JS flow?

  3. If I need to do a few database calls with my asynchronous methods (different tables). Could I have a conflict if i make two database calls at the same time because make my AJAX asynchronous? I am quite new to asynchronous calls. (SQL database) (Spring Boot Java 8)

The basic flow would be something like this:

Basic flow img

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Grismak
  • 192
  • 16
  • 1
    1. you handle it in your code. 2. synchronous calls are deprecated, so never make a synchronous ajax call - the first A in AJAX actually stands for Asynchronous, so a Synchronous AJAX request makes no sense anyway (though of course it is a thing) 3. can't help you with that, I don't know java, all that would be handled in Java and has nothing to do with AJAX as such – Jaromanda X Jul 14 '22 at 05:56
  • well. this only show how ignorant i am to JS. thanks for the answers, the second one is really a wake up call – Grismak Jul 14 '22 at 05:57
  • 2
    the reason synchronous AJAX is deprecated in the browser is the negative impact on user experience - in browsers the page literally freezes until the response comes back ... imagine getting data that takes a few seconds or more - it'd be a bad experience – Jaromanda X Jul 14 '22 at 06:08
  • thanks for all the info about AJAX, i will start studying it now that i will have to work more with it – Grismak Jul 14 '22 at 06:15
  • But ... synchronous AJAX could be OK in a web workers - it's only on the "main thread" where it is deprecated - as far as I am aware – Jaromanda X Jul 14 '22 at 06:26
  • 1
    As for 3, it is the same in Java as most other systems: the backend may process your calls in a different order or even truly in parallel (think a cluster, one call is processed from one node, the other call fro another node). So it depends on your semantics: if one depends on the outcome of the other, then you should make sure they are only called sequentially. If they both contribute independently to the final outcome, then you have to make sure to revert the effects of the one, if the other fails. There are many ways to do that - and maybe it should be a responsibility of the backend. – Nikos Paraskevopoulos Jul 14 '22 at 07:18
  • Thanks,both of you. With this I have the three questions answered, I will post them as an answer in order to close the post. Thank you so much for your help, I learned a few new things today. – Grismak Jul 14 '22 at 07:47

1 Answers1

0

Ths answer is a compilation of everything told in the comments, not my own.

  1. You handle it in your code.

  2. Synchronous calls are deprecated, so never make a synchronous ajax call - the first A in AJAX actually stands for Asynchronous, so a Synchronous AJAX request makes no sense anyway (though of course it is a thing)

  3. It is the same in Java as most other systems: the backend may process your calls in a different order or even truly in parallel (think a cluster, one call is processed from one node, the other call fro another node). So it depends on your semantics: if one depends on the outcome of the other, then you should make sure they are only called sequentially. If they both contribute independently to the final outcome, then you have to make sure to revert the effects of the one, if the other fails. There are many ways to do that - and maybe it should be a responsibility of the backend.

Grismak
  • 192
  • 16
  • This may need more explanation for clarity and maybe links to resources from which this where inferred. – zaghadon Sep 02 '22 at 06:16