当前位置:网站首页>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
边栏推荐
- Understand the key points of complement
- [Niuke challenge 47] C. conditions (BitSet acceleration Floyd)
- Anaconda3 installation
- Intuitive understanding entropy
- Reading integrity monitoring techniques for vision navigation systems - 5 Results
- FileProvider 路径配置策略的理解
- The songbird document editor will be open source: starting with but not limited to markdown
- Facing the global market, platefarm today logs in to four major global platforms such as Huobi
- MySQL分区表实现按月份归类
- Manjaro installation and configuration (vscode, wechat, beautification, input method)
猜你喜欢
Cumcm 2021 - B: préparation d'oléfines C4 par couplage éthanol (2)
Visual Road (XII) detailed explanation of collection class
数据库管理软件SQLPro for SQLite for Mac 2022.30
使用 PHP PDO ODBC 示例的 Microsoft Access 数据库
关于JUC三大常用辅助类
Diary of dishes | Blue Bridge Cup - hexadecimal to octal (hand torn version) with hexadecimal conversion notes
Visual common drawing (I) stacking diagram
Introduction to data analysis 𞓜 kaggle Titanic mission (IV) - > data cleaning and feature processing
The songbird document editor will be open source: starting with but not limited to markdown
STM32接电机驱动,杜邦线供电,然后反烧问题
随机推荐
JDBC – PreparedStatement – 如何设置 Null 值?
CUMCM 2021-b: preparation of C4 olefins by ethanol coupling (2)
Visual common drawing (III) area map
Excel · VBA custom function to obtain multiple cell values
Code implementation of general bubbling, selection, insertion, hill and quick sorting
How to use JDBC callablestatement The wasnull () method is called to check whether the value of the last out parameter is SQL null
26. 删除有序数组中的重复项
MIT:用无监督为世界上每个像素都打上标签!人类:再也不用为1小时视频花800个小时了
Read integrity monitoring techniques for vision navigation systems
Difference between pregnancy box and delivery box
Solution architect's small bag - 5 types of architecture diagrams
Visualization Road (11) detailed explanation of Matplotlib color
UDP basic learning
colab
Using El popconfirm and El backtop does not take effect
Visual common drawing (I) stacking diagram
Latex usage
About the three commonly used auxiliary classes of JUC
期货开户哪个公司好?安全靠谱的期货公司谁能推荐几家?
Ueditor -- limitation of 4m size of image upload component