I encountered an interview question today, that is, if there are two asynchronous ajax operations on the page, because the execution order of the two asynchronous operations is not sure, how to execute a new operation after both operations are executed? What's the best approach?
I answered method one at that time: nest two ajaxes and perform new operations in the return function of the second ajax. Interviewer's reply: This method is too lame.
So I thought about the second answer: monitor local variables through the timer setTimeout, and ensure that the two asynchronous operations are completed before performing a new operation. Interviewer's reply: The performance of this method is not good. Can you think of a simple and more reasonable method?
Thoughts failed at the time
So I put this question up to find what is the best way? Welcome to discuss and give advice
1. Promise wraps asynchronous ajax operations,
2. Define async function,
3. Use await to wait for the asynchronous acquisition of promise data to be completed. This method is simple and efficient. Please see the sample code I wrote specifically for you below. I am too lazy to use it. After ajax obtains data, use the settimeout function to simulate obtaining data. This function is asynchronous and has the same principle and effect.
http://api.jquery.com/jQuery....
The current out-of-the-box native method in the browser environment is Promise.all.
Take calling my map library Sinomap Demo as an example. In order to load a map on this page, multiple requests are required to be initiated at the same time but the return order cannot be guaranteed:
China terrain data
JSON data of each province
When multiple charts are overlaid, there are multiple JSON data in multiple charts that need to be returned through different data interfaces...
The solution is directly in the unpackaged
http://sinomap.ewind.us/demo/demo.js
, example:In this way, Promise not only realizes the decoupling of callback logic, but also realizes basic asynchronous process control.
I just saw the when method of jquery, so I rewrote one for you. It may not be as good as jquery, but at least it can achieve the effect. You can directly enter the following code in the console to try. Let me go and write It took me half an hour. .
Ask if I can use jQ. If so, just say:
By the way, here is a document reference for $.when
I think it’s Promise’s method all or something
Your question is there are three things a, b, c. c should be executed after a and b are completed.
There are many methods: such as the nesting method you mentioned, and the violent monitoring method
I have thought about this question before, and here is my answer.
Asynchronous emitter
How to perform asynchronous operations using array storage
Note that the functions inside all have a parameter commit , which is a function used to return a value. When ajax succeeds, just pass the return value back in
Writing a launcher
Processors are data like todos. cb is the final callback.
Concurrent them
Execute an asynchronous emitter and provide
final callback
Link
https://eczn.coding.me/blog/%...
You can define a variable a=0, and set a++ in the callback after the ajax request is successful;
Then judge a==2 in both callbacks and execute the operation function
Set two flags, and then two ajax call the same callback. In this callback, it is judged that both flags are true before subsequent operations are performed.
Write ajax in another ajax and execute it in the callback