If we want to call a function, send a network request in this function.
- If sending the network request is successful, inform the caller that the sending is successful, and return the relevant data ;
- If the network request fails to be sent, the caller is notified of the failure and an error message .
If there is no Promise, you can indeed implement it yourself and get the corresponding callback. but,
- We need to design the callback function by ourselves , the name of the callback function, and the use of the callback function;
- Different people and different frameworks design different solutions. It is very troublesome for us to patiently read other people's source code or documents .
If you can define a standard and give the caller a promise, you can call back the data. Developers can use uniform standards to write similar codes, reducing learning costs. So Promise of ES6 is used to solve the dilemma of asynchronous code.
Table of contents
3. The difference between different values of resolve
1. Understanding of Promise
Promise is a class that can be translated into promises , promises, and contracts;
When we need it, we can give the caller a promise : call back data to the caller after a period of time . In this case, a Promise object can be created;
When creating a Promise object through new, we need to pass in a callback function, which we call executor
- This callback function will be executed immediately, and the other two callback functions resolve and reject will be passed in ;
- When we call the resolve callback function , the callback function passed in by the then method of the Promise object will be executed ;
- When we call the reject callback function , the callback function passed in by the catch method of the Promise object will be executed ;
const promise = new Promise((resolve, reject)=>{ //调用resolve,那么then传入的回调会被执行 resolve("成功信息") //调用reject,那么catch传入的回调会被执行 reject("错误信息") }) promise.then(res => { console.log(res) }).catch(err => { console.log(err) })
During the use of Promise, we can divide it into three states:
- When executing the code in the executor , it is pending : the initial state, neither honored nor rejected;
- When resolve is executed , it is in the fulfilled state, and the Promise has been fulfilled: it means that the operation is successfully completed;
- When reject is executed , it is in the rejected state, and Promise has been rejected: it means that the operation failed.
2. Callback function Executor
Executor is a callback function that needs to be passed in when creating a Promise. This callback function will be executed immediately , and two parameters are passed in:
new Promise((resolve, reject) => {
console.log("执行executor代码")
})
- Through resolve , the status of ( fulfilled ) Promise can be fulfilled , we can also call it resolved (resolved);
- Through reject , you can reject ( reject ) the state of Promise;
- When we call resolve , if the value passed by resolve itself is not a Promise , then the state of the Promise will be changed to fulfilled (fulfilled) ;
- When we call reject later, there will no longer be any response (not that this line of code will not be executed, but that the Promise state cannot be changed);
3. The difference between different values of resolve
Case 1: If an ordinary value or object is passed in , this value will be used as the parameter of the then callback ;
new Promise((resolve, reject) => {
resolve("normal resolve")
}).then(res =>{
console.log("res:", res)
})
Case 2: If another Promise is passed in , the new Promise will determine the state of the original Promise ;
new Promise((resolve, reject) => {
resolve(new Promise((resolve, reject) => {
setTimeout(() => {
resolve("第二个Promise的resolve")
}, 3000)
}))
}).then(res => {
console.log("res:",res)
}).catch(err => {
console.log("err:",err)
})
Case 3: If an object is passed in to resolve , and the object implements the then method , then the then method will be executed , and the state of the Promise will be determined according to the result of the then method :
new Promise((resolve, reject)=>{
resolve({
then: function(resolve, reject) {
resolve("thenable value")
}
})
}).then(res => {
console.log(res)
})
4.then method
The then method is a method (instance method) on the Promise object:
It is Promise.prototype.then placed on Promise's prototype
The then method accepts two parameters:
The callback function of fulfilled : the function that will be called back when the status becomes fulfilled;
Reject callback function : a function that will be called back when the state becomes reject;
const promise = new Promise((resolve, reject) => {
resolve("resolve")
reject("reject")
})
promise.then(res => {
console.log("res:", res)
}, err => {
console.log("err:", err)
}
)
//等价于
promise.then(res => {
console.log("res:", res)
}).catch(err => {
console.log("err:", err)
})
Although the then method can accept two parameters, it is more readable to use the .catch method.
- We can pass in the corresponding fulfilled callback for each call ;
- When the state of Promise becomes fulfilled , these callback functions will be executed ;
promise.then(res=>{
console.log("res1:", res)
})
promise.then(res=>{
console.log("res2:", res)
})
promise.then(res=>{
console.log("res3:", res)
})
5.catch method
- We can pass in the corresponding reject callback for each call ;
- When the state of Promise becomes reject, these callback functions will be executed ;
promise.catch(err => {
console.log("err1:", err)
})
promise.catch(err => {
console.log("err2:", err)
})
promise.catch(err => {
console.log("err1:", err)
}).catch(err => {
console.log("err2:", err)
}).then(res => {
console.log("res:", res)
})
promise.catch(err => {
console.log("err1:", err)
throw new Error("error message")
}).then(res => {
console.log("res:", res)
}).catch(err => {
console.log("err2:", err)
})
6. finally method
const promise = new Promise((resolve, reject) => {
resolve("resolve")
reject("reject")
})
promise.then(res => {
console.log("res:", res)
}).catch(err => {
console.log("err:", err)
}).finally(() =>{
console.log("finally")
})
7. The resolve method
Promise.resolve("这样会简洁一点")
//等价于
new Promise((resolve) => {
resolve("fulfilled")
})
8. reject method
Promise.reject("简洁")
//等价于
new Promise((resolve, reject) => {
reject("reject")
})
9. all method
const promise1 = new Promise((resolve, reject) => {
resolve("resolve")
reject("reject")
})
const promise2 = new Promise((resolve, reject) => {
resolve("resolve")
reject("reject")
})
const promise3 = new Promise((resolve, reject) => {
resolve("resolve")
reject("reject")
})
Promise.all([promise1, promise2, promise3]).then(res => {
console.log(res)
}).catch(err => {
console.log(err)
})
10. allSettled method
//创建三个promise promise1 promise2 promise3
Promise.allSettled([promise1, promise2, promise3]).then(res => {
console.log(res)
}).catch(err => {
console.log(err)
})
11. race method
//创建三个promise promise1 promise2 promise3
Promise.race([promise1, promise2, promise3]).then(res => {
console.log(res)
}).catch(err => {
console.log(err)
})
12. any method
//创建三个promise promise1 promise2 promise3
Promise.any([promise1, promise2, promise3]).then(res => {
console.log(res)
}).catch(err => {
console.log(err)
})