当前位置:网站首页>Promise details
Promise details
2022-04-23 11:04:00 【Mu_ Mu is a little white】
1. Basic concepts :
Promise yes JS Important concepts in asynchronous programming , Asynchronous abstract processing objects , It's popular at present Javascript One of the asynchronous programming solutions
2.Promise Three states of :
pending: Object initialization status
fulfilled: When calling resolve( success ), Will be pending => fulfilled
rejected: When calling reject( Failure ), Will be pending => rejected
3. establish Promise
3.1 new Promise(resolve, reject);
const promise = new Promise((resolve, reject) => {
resolve('fulfilled'); // State by pending => fulfilled
//reject('rejected'); // State by pending => rejected
});
//then stay Promise Trigger when the state changes
promise.then(result => { // onFulfilled resolve Called will enter
console.log(result); },
reason => { // onRejected ,reject Called will enter
})
3.2 c Static method creation
3.2.1 Promise.resolve() Return to one fulfilled State of promise object
3.2.2 Promise.reject() Return to one rejected State of promise object
Promise.reject() No matter what value is passed to it , It will pass this value directly to the error callback function .
Promise.resolve() How to execute depends on the parameters passed .
1) Pass common type ( Values, etc )
let p1 = new Promise(resolve => {
resolve(' success ');
})
// It's equivalent to the above
let p2 = Promise.resolve(' success '); // It will directly determine success and pass this value to
2)Promise type :
let param = new Promise( resolve =>{
resolve('----resolve----')
})
// Directly return the passed in promise
let p = Promise.resolve(param); // Direct will param The instance is returned to p
p.then(data => {console.log(data)}) // After printing, the result is ----resolve----
console.log( p === param) // Print true, Although in p.then But because Promise Asynchronous execution will print first true

3) Pass a thenable object
let obj = { //obj It's a thenable object
then(callback){
console.log('-----then---------');
callback('-------callback-----')
},
other(){
console.log('--------other---------') // No printing
}
}
Promise.resolve(obj)
Execution results :

Promise.resolve(obj).then(data => {
console.log(data)
})
Execution results :

Conclusion : Spread thenable Object time , Will be executed immediately then Method , If then Method has a callback , Will be in Promise.resolve(obj).then() Called when the ( I personally understand that at this time then The anonymous function inside data => {
console.log(data)
} Replaced the callback).
版权声明
本文为[Mu_ Mu is a little white]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231053252107.html
边栏推荐
- Idea - indexing or scanning files to index every time you start
- CUMCM 2021-b: preparation of C4 olefins by ethanol coupling (2)
- 期货开户哪个公司好?安全靠谱的期货公司谁能推荐几家?
- Notes on concurrent programming of vegetables (V) thread safety and lock solution
- 数据库管理软件SQLPro for SQLite for Mac 2022.30
- JVM - common parameters
- Dirichlet prefix sum (number theory optimization formula sub complexity weapon)
- Jupyter Lab 十大高生产力插件
- Visual common drawing (V) scatter diagram
- Constraintlayout layout
猜你喜欢

Manjaro installation and configuration (vscode, wechat, beautification, input method)

第六站神京门户-------手机号码的转换

Notes on concurrent programming of vegetables (V) thread safety and lock solution

More reliable model art than deep learning

ConstraintLayout布局

After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before

使用zerotier让异地设备组局域网

Visual common drawing (V) scatter diagram

MySQL Router重装后重新连接集群进行引导出现的——此主机中之前已配置过的问题

数据库管理软件SQLPro for SQLite for Mac 2022.30
随机推荐
mysql创建存储过程及函数详解
Chapter 1 of technical Xiaobai (express yourself)
学习 Go 语言 0x08:《Go 语言之旅》中 练习使用 error
Code implementation of general bubbling, selection, insertion, hill and quick sorting
VIM usage
Pytorch implementation of transformer
MySQL面试题讲解之如何设置Hash索引
MBA-day5數學-應用題-工程問題
Mysql8.0安装指南
软件测试人员,如何优秀的提Bug?
Google Earth Engine(GEE)——将原始影像进行升尺度计算(以海南市为例)
Solution architect's small bag - 5 types of architecture diagrams
remote: Support for password authentication was removed on August 13, 2021.
colab
Mba-day6 logic - hypothetical reasoning exercises
Typora operation skill description (I)
高价买来的课程,公开了!phper资料分享
一个微博数据库设计带来的简单思考
Mba-day5 Mathematics - application problems - engineering problems
Visual solutions to common problems (VIII) mathematical formulas