当前位置:网站首页>What is promise? What are the benefits of promise
What is promise? What are the benefits of promise
2022-07-24 00:34:00 【Ssie-】
1、 What is? Promise
promise Is the solution to asynchrony , It's essentially a constructor , You can instantiate an object with it . The object has resolve、reject、all, Prototype has then、catch Method .promise Objects have three states :pending( Have in hand )、fulfilled( success )、rejected( Failure )
Promise The object has two characteristics :
(1) The state of the object is not affected by the outside world .Promise The object represents an asynchronous operation , Only the result of asynchronous operation can determine Promise Object state , No other operation will change this state . This can be compared with Promise Its name is understood together ,Promise yes “ promise ” It means , Says nothing else can be changed .
(2) Once the status changes , Will not change again . And its state changes only by (pending->fulfilled、
pending->rejected) And as long as one of these two situations occurs , The state is fixed .
2、Promise The advantages of :
(1) Support chain calls ( Asynchronous operations can be displayed synchronously ) Avoid going back to hell
What is callback hell ?
Multiple callback functions are nested layer by layer , The result returned asynchronously by the outer callback function is the execution condition of the inner callback function
The drawback of callback hell ?
1、 Readability 、 Poor maintainability
2、 Not convenient for exception handling
What is a callback function ?
The callback function is abbreviated as “ Callback ”, It can be understood as “ Go back and call the function ”. It is taken as a parameter , Pass it to another function , After that function is executed , Then execute the parameter passed in .
Why should there be a callback function ?
Because you don't know when the sub thread will finish executing , At this time, you need a callback function , Notify the main thread after it finishes executing .
(2) Specifying callback functions is more flexible : Previously, we had to specify a callback before defining an asynchronous task , Now you can specify the callback after the asynchronous task is executed .
Promise The shortcomings of ?
1、 Cannot cancel it , Once established, it will be executed immediately , There is no way to cancel halfway
2、 If you don't set a callback function for it , Internal thrown errors cannot be displayed
3、 When in pending In state , It's impossible to know where the current progress is ( Is it just beginning or just ending )
( Because I am right Promise I haven't really understood , So record )
边栏推荐
- Accelerating matrix vector multiplication of special matrices with FFT
- 数据模型设计方法概述
- Adaptation scheme of large screen visualization
- Notes and Thoughts on the red dust of the sky (VI) due to lack
- GBase 8c 访问权限查询函数(五)
- Intelligent OCR identification of express documents helps the logistics industry to upgrade Digitalization
- sed 深入理解与使用
- docker 拉取redis镜像 并运行
- GBase 8c模式可见性查询函数(二)
- Résumé du websocket minier
猜你喜欢

Classic examples of C language switch case statement conversion date format

The implementation in Oracle replaces the specified content of the specified column with the desired content

Docker pulls the redis image and runs it

Distributed cap principle

Classic example of C language - commodity inspection code

php实现 Stripe订阅

Classic example of C language - convert the input two digits into English

数仓数据指标和标签体系区别

Mobile terminal H5 - a lifeline timeline

Easy gene | target gene DNA methylation sequencing (target BS)
随机推荐
Redis主从同步机制
Blockbuster | certik: Web3.0 industry safety report release in the second quarter of 2022 (PDF download link attached)
Classic example of C language - print the input two digits in reverse order
理解多态,让不同的“人”做同一件事情会产生不同的结果
Printf function - conversion description
GBase 8c 会话信息函数(六)
Gbase 8C access authority query function (6)
Classic example of C language - find the minimum number of banknotes
GBase 8c 会话信息函数(四)
C language macro definition
Redis持久化机制RDB、AOF
数据模型设计方法概述
采坑websocket總結
Gbase 8C access authority query function (III)
Method of C language annotation
English grammar_ Demonstrative pronoun - so
JS drag and drop element
网络系统实验:ping不通的问题解决
mongodb的多数据源配置
Don't let Fujin Yibo see this