The idea of synchronization is ： All the operations are done , To return it to the user . So users wait too long online , Give users a feeling of being stuck （ It's system migration , Click migration , The interface doesn't move , But the program is still running , The feeling of being stuck ）. In this case , The user cannot close the interface , If it's turned off , That is, the migration program is interrupted .
Put user requests in message queue , And feed back to users , System migration has started , You can close the browser . Then the program slowly writes to the database . This is asynchronous . But users don't feel stuck , I will tell you. , Your request system has responded . You can close the interface .
Asynchronous example ：（ With AJAX For example ）
Synchronization and asynchrony are relative in themselves
Synchronization is like When the client sends a request to the server , While waiting for the server to respond to the request , The client doesn't do anything else . When the server is finished, it returns to the client . In this case, the client needs to wait all the time . Users will be unfriendly to use .
Asynchronous is , When the client sends a request to the server , The server is waiting for the response , The client can do other things , This saves time , Improved efficiency .
There is a reason for being Although asynchronous is good But some problems need to be solved by synchronization , For example, what we need is to get the returned data for operation . These are things that asynchrony can't solve .
If the return value of a function is in
Internal , So this function is asynchronous
Such as ： array.forEach(n=>console.log(n)) It's synchronous callbacks Copy code
How to solve these three problems ？