without considering syntax such as async.
- 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.
- 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:)