What is synchronization

I can get the results straight away

What is asynchrony

You can’t get the results directly

An asynchronous function

A function is asynchronous if its return value is inside setTimeout, AJAX (XMLHttpRequest), and AddEventListener

Note: Do not set AJAX to sync

The asynchronous process can be callback or polling

Promise

Promise’s purpose Front-end solution to asynchronous problems is to avoid callback regions and make code look more synchronous

How to create a New Promise

return new Promise((resolve,reject)=>{})
Copy the code

How to use promise.prototype.then

const promise1 = new Promise((resolve, reject) => { resolve('Success! ') }) promise1.then((value) => { console.log(value) })Copy the code

Promise.catch is used the same way as then, executing this then then that,

How do I use promise.all

let Promise1 = new Promise(function(resolve,reject)=>{}) let Promise2 = new Promise(function(resolve,reject)=>{}) let Promise3 = new Promise(function(resolve,reject)=>{}) let p = Promise.all([Promise1,Promise2,Promise3]) Function (){},function(){})Copy the code

How do I use promise.race

const promise1 = new Promise((resolve, reject) => { setTimeout(resolve, 500, 'one') }) const promise2 = new Promise((resolve, reject) => { setTimeout(resolve, 100, 'two')}) promise.race ([promise1, promise2]). Then ((value) => {console.log(value)})Copy the code