当前位置:网站首页>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 )
边栏推荐
- GBase 8c 访问权限访问函数(四)
- Codeforces Round #807 (Div. 2)(A-D)
- Redis cluster hash sharding algorithm (slot location algorithm)
- The implementation in Oracle replaces the specified content of the specified column with the desired content
- Redis分布式锁待续
- docker 拉取redis镜像 并运行
- AWS Article 3 how to publish message to IOT mqtt in go language
- GBase 8c 二进制字符串操作符
- Comparison of the shortcomings of redis master-slave, sentinel and cluster architectures
- [video game training] non contact object size and shape measurement 2020 video game G
猜你喜欢

Flutter | specifies the type of page return value

docker 拉取redis镜像 并运行

Network system experiment: solve the problem of Ping failure

【电赛训练】非接触物体尺寸形态测量 2020年电赛G题

Delete all data of specified query criteria in Oracle

EFCore高级Saas系统下单DbContext如何支持不同数据库的迁移

如何提升数据质量

网络系统实验:ping不通的问题解决

Overview of data model design method

理解多态,让不同的“人”做同一件事情会产生不同的结果
随机推荐
分布式之 CAP 原则
Classic example of C language - convert the input two digits into English
【低代码】低代码发展的局限性
Pytest interface automation test framework | summary
EFCore高级Saas系统下单DbContext如何支持不同数据库的迁移
Pytest interface automation test framework | control test case execution
MySQL之数据查询(SELECT)
Detailed overview of data standards -2022
Classic examples of C language - adding two scores
Summary of polynomial commitment schemes
Redis master-slave synchronization mechanism
Inode, soft link, hard link
Redis主从同步机制
Pytest interface automated testing framework | how to get help
GBase 8c 位串操作符(一)
High number_ Chapter 2 differential calculus of multivariate functions__ Geometric application of partial derivatives_ Tangent and normal plane of space curve
php实现 Stripe订阅
Classic example of C language - commodity inspection code
Development of main applet for business card traffic near the map
GBase 8c 会话信息函数(二)