background

Asynchronous or synchronous

Synchronous:

The idea of synchronization is that all operations are done before it is returned to the user. In this way, the user waits too long online, giving the user a feeling of stuck dead (that is, in the system migration, click the migration, the interface does not move, but the program is still executing, stuck dead feeling). In this case, the user cannot close the interface; if so, the migration will be interrupted.

Asynchronous:

Put the user request on the message queue and report back to the user that the system migration has started and you can close the browser. The program then slowly writes to the database. This is asynchrony. But the user doesn’t feel stuck and will tell you that your request has been answered. You can close the interface now.

Asynchronous example :(take AJAX as an example)

  • After request.send(), there is no direct response
  • Have to wait till after readyState into 4, the browser back call request. The onreadystatechange function
  • Then we can get request. Response

Synchronization and asynchrony are relative in themselves

Synchronization means that when a client sends a request to a server, the client does nothing while waiting for the server to respond to the request. Return to the client when the server is done. In this case, the client has to wait. Users will be unfriendly to use.

Asynchrony means that when a client sends a request to a server, the client can do something else while waiting for the server to respond. This saves time and improves efficiency.

It’s there for a reason asynchronous is good but there are some problems that need to be solved with synchronization, for example there are things that we need to get the data back and operate on. These are things that asynchrony cannot solve.

Determine synchronous asynchronous

If the return value of a function is

  • setTimeout
  • AJAX, XMLHttprequest (don’t set AJAX to sync, it will make the page stuck during the request)
  • addEventListener
  • To be continued

Then the function is an asynchronous function

Callback

  • I’m writing to somebody else’s function
  • I’ll invite you to dinner later. I’ll invite you to dinner later.

The relationship between asynchrony and callback

associated

  • The asynchronous task needs to notify JS to get the result when it gets it
  • How to notify?
  • You can ask JS to leave a function address (phone number) to the browser
  • When the asynchronous task is complete, the browser calls the function address.
  • Pass the result as a parameter to the function.
  • This function is written for browser calls, so it is a callback function

The difference between

  • An asynchronous task can be notified of its results using a callback function, or it can be obtained through polling, which is the process of periodically asking if the results have been obtained.
  • But callbacks don’t have to be used exclusively for asynchronous tasks
For example, array.forEach(n=>console.log(n)) is a synchronous callbackCopy the code

Disadvantages of callbacks

  • Not standard, the name is multifarious
  • It’s easy to get callback hell and the code becomes unreadable
  • Error handling is difficult

How to solve these three problems?

  • Specifies the name or order of the callback
  • Refuse to call back to hell to make the code more readable
  • Easy to catch errors