Ajax Synchronize or asynchronous?

frontend developers have been in contact for some time, and have been trying to figure out a problem about ajax Synchronize asynchronism. Asynchrony cannot ensure the successful return of data A, so it cannot be processed where An is needed. If Synchronize request is used, it loses the advantage that asynchronous request does not wait quickly, and the program continues to execute after the value is returned.
my idea is that some js frameworks can listen for object properties, and some functions that rely on returning data can be placed in the listening function and executed when the listening property has a value.
I don"t know if there are other better ways to deal with it. Those who have the same doubts can discuss it together. I hope the gods will not hesitate to tell me

.
Mar.10,2021

without considering syntax such as async.

  1. Asynchronous does not return a value (or the return value is not the result you want), which means that there is no guarantee that asynchronous requests return data in order with other logic.
    however, asynchronous operations are generally bound to the callback function . All methods that rely on the returned results are performed in the callback function. Of course, you can assign the returned results to the outer variables in the callback.
    at the same time, the listening object properties you mentioned are also asynchronous operations, but some of the responsibilities in the original function callback are transferred to the listening callback, because you can already make these dependency return worthy function calls in the original function callback.
  2. Synchronize ajax requests are rarely used, and the only time they have been used is when making access records and ensuring that the request is sent successfully before the page closes.

I don't know if it's what you need. Jquery has a deffered object.

$.when($.ajax("/echo/html/"), $.ajax("/echo/xml/"))

.done(function(){ alert(""); })

.fail(function(){ alert(""); });

Asynchronous cannot ensure that data An is returned successfully, and cannot be processed where An is needed

there are only a few situations in which this can be encountered. Compared with this disadvantage, the disadvantage that Synchronize will make the page unresponsive is obviously more serious. The use of Synchronize is no longer encouraged.

in fact, asynchronous processing combined with Promise, can also be a lot easier.


you need to get the returned data before processing. After the ajax request is successful, you can deal with the logic that requires A data, callback function, and Promise are all solutions


you can give a loading animation when loading data, refresh the display after loading successfully, and give the corresponding prompt when loading fails

    load() {
      // 
      ajaxFetchData()
        .then(data => {
          // 
        })
        .catch(err => {
          // 
        })
        .finally(() => {
          // 
        });
    }

synthesize your reply, very fruitful. I also blame myself for not going to jquery API for a long time. All in all, thank you:)

Menu