The difference between synchronous and asynchronous and Promise usage scenarios

Javascript's single thread means that there is only one thread responsible for executing code in the JS execution environment. This is safer and simpler, but the disadvantages are also obvious. If you encounter a particularly time-consuming task, all subsequent tasks must To queue up and wait for the end of this task will cause the entire operation to be delayed, and there will be a situation of suspended animation. In order to solve this problem, javascript divides the execution mode of the task into two modes, namely synchronous mode (Synchronous) and asynchronous mode (Asynchronous)

Sync mode

When it is executed, it will be pressed into the calling cup, which will record the task currently being executed, and disappear from the cup after the execution. When a task takes a long time, the subsequent tasks are easy to block. Single-threaded JavaScript language cannot handle a large number of time-consuming tasks at the same time

Asynchronous mode

Asynchronous mode does not wait for the end of this task to start the next task. For time-consuming tasks, the next task will be executed immediately after being opened. The subsequent logic will generally be defined by a callback function. When the task is completed, the callback function will be executed automatically.

JavaScript is single-threaded, but the browser is not single-threaded. Some APIs of JavaScript are not single-threaded by calling some javascript APIs, such as a countdown timer. There is a separate thread inside it to take charge of the countdown. After the time is up, the callback is put To the message queue, that is to say, such a thing is done by a separate thread. When we talk about single thread, we mean the thread of code that is executed. The API provided by the running environment works in a synchronous or asynchronous mode.

Asynchronous method

There are currently ways to implement asynchronous, custom nesting, Promise, generator, Deffferd, and ES7 async (actually a generator package). Different scenarios can choose different ways to achieve


What is Promise

Definition of promise


First, give a description of the academic point of Promise: A promise represents the execution return state of an asynchronous operation, and this execution return state may not be known when the promise object is created. It allows you to specify processing methods for the success or failure of asynchronous operations.

The purpose of promise

Promise can be used to avoid the problem of nested callback (callback hell) in asynchronous operation functions, because the most direct way to solve asynchronous is to nest callbacks. Put the latter operation in the asynchronous callback of the previous operation, but if there are many operations , There will be many levels of nesting.

Knowledge points of promise

1. The function of the then method of the Promise instance is to add a callback function when the state of the Promise instance changes. The first parameter of the then method is the callback function in the resolved state, and the second parameter is the callback function in the rejected state.

2. The then method returns a new Promise instance (note: not the original Promise instance). Therefore, chain writing can be used.

3. The error of the Promise object has a "bubble" nature and will be passed backwards until it is caught. That is, the error will always be caught by the next catch statement.

4. Generally, do not define a callback function in the rejected state (that is, the second parameter of then) in the then method, but always use the catch method. Because catch is closer to synchronous writing.

5. The catch method returns a Promise object, you can then call the then method.

6. In JavaScript functions, only return / yield / throw will interrupt the execution of the function, and nothing else can prevent it from running to the end. This is also the so-called Run-to-completion feature.

Pros of promise

  1. One of the great advantages of Promises is the immutability of results. Once the value of the Promise is determined to be fulfilled or rejected, the value of the obtained Promise object will be the same no matter how long it takes.

Understanding of immutability:

Like resolve/reject, it’s just a callback, and the so-called immutability just means that when the first resolve/reject is encountered, the Promise is tagged with a tag based on its result and cannot be changed. Why continue to do it, let's not do this Promise thing.

Precautions for using promises

1. If you always write logic in the Promise constructor, even if there is an unexpected input, you can return a Rejected Promise in most cases

2. In the Promise constructor, unless you clearly know the correct posture to use throw, please use reject.

3. Compatibility with Promise and Callback is indeed a great thing. Please understand the principle as much as possible before using third-party code. You can write one yourself if it is short. Although Promise is good, don't use it indiscriminately. Keep in mind that it will swallow the risk of errors in real time.

Guess you like

Origin blog.csdn.net/weixin_53687450/article/details/114991516