当前位置:网站首页>什么是Promise?Promise有什么好处
什么是Promise?Promise有什么好处
2022-07-23 09:38:00 【Ssie-】
1、什么是Promise
promise是解决异步的方法,本质上是一个构造函数,可以用它实例化一个对象。对象身上有resolve、reject、all,原型上有then、catch方法。promise对象有三种状态:pending(进行中)、fulfilled(成功)、rejected(失败)
Promise对象有两个特点:
(1)对象的状态不受外界的影响。Promise对象代表一个异步操作,只有异步操作的结果可以决定Promise对象的状态,其他任何操作都不会改变这个状态。这个可以与Promise它的名字一起理解,Promise是“承诺”的意思,表示其他手段无法改变。
(2)状态一旦改变,便不会再次改变。而且它的状态改变只会由(pending->fulfilled、
pending->rejected)并且这两种情况只要发生其中一个,状态便固定了。
2、Promise的优点:
(1)支持链式调用(可以将异步操作以同步的方式显示出来)避免回调地狱
什么是回调地狱?
多个回调函数层层嵌套,外层回调函数异步返回的结果是内层回调函数执行的条件
回调地狱的缺点?
1、可读性、可维护性差
2、不便于异常处理
什么是回调函数?
回调函数简称为“回调”,可以理解为“回过头来调用的函数”。它是作为一个参数,传递给另一个函数,待那个函数执行完毕之后,再来执行这个传递进来的参数。
为什么要有回调函数?
因为你不知道子线程什么时候执行完毕,这个时候就需要有回调函数,待它执行完毕之后再来通知主线程。
(2)指定回调函数的方式更加灵活:先前我们必须在定义一个异步任务之前指定回调,现在可以在异步任务执行完毕之后再指定回调。
Promise的缺点?
1、无法取消它,一旦建立就会立即执行,没办法中途取消
2、如果不给它设置回调函数,内部的抛出错误没办法显示
3、当处于pending状态的时候,无法得知当前进展到哪一地步(是刚开始还是刚结束的状态)
(由于自己对Promise一直都没有特别懂,因此记录一下)
边栏推荐
- (heavy chain dissection) Magic Tree
- The self-developed data products have been iterated for more than a year. Why not buy third-party commercial data platform products?
- Which is a good fixed asset management system? What are the fixed asset management platforms?
- Sword finger offer19 regular expression
- Qt| imitation text floating letter
- R语言实战应用案例:绘图篇(三)-多种组合图型绘制
- Cmake notes
- C thread lock and single multithreading are simple to use
- C语言入门实战(11):输入一组正整数,求逆序数的和
- C language project practice: 24 point game calculator (based on knowledge points such as structure, pointer, function, array, loop, etc.)
猜你喜欢

Towhee weekly model

Dynamic programming -- knapsack problem
![[applet automation minium] i. framework introduction and environment construction](/img/1f/95b78e6574c3af3ff7abcf5db838f5.png)
[applet automation minium] i. framework introduction and environment construction
![Looking for peak [Abstract dichotomy exercise]](/img/99/122e79784f0f07120680d2cbcf89da.png)
Looking for peak [Abstract dichotomy exercise]

微信官方出品!小程序自动化框架 minium 分享预告

运维高级作业03

Palindrome related topics

Oracle 报表常用sql

mysql 之general_log日志

Regular expression common syntax parsing
随机推荐
Cmake notes
Is online handling of fund account opening safe? Who can answer it
@FeignClient使用詳細教程(圖解)
LZ77 file compression
452. Detonate the balloon with the minimum number of arrows
如何实现多个传感器与西门子PLC之间485无线通讯?
Feignclient utilise un tutoriel détaillé (illustration)
21 - vertical traversal of binary tree
Openharmony South learning notes - hi3861+hc-sr04 ultrasonic testing
ArgoCD 用户管理、RBAC 控制、脚本登录、App 同步
Reflection invokes transaction methods, resulting in transaction invalidation
【数组&&字符串&&宏练习题】
The self-developed data products have been iterated for more than a year. Why not buy third-party commercial data platform products?
What methods are called behind the use of objects
Using JS to parse and execute XSS automatically
Argocd user management, RBAC control, script login, APP synchronization
【C語言】猜數字小遊戲+關機小程序
PKI体系快速介绍
Using shell script to block IP with high scanning frequency
【小程序自动化Minium】一、框架介绍和环境搭建