当前位置:网站首页>什么是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一直都没有特别懂,因此记录一下)
边栏推荐
- CAN总线快速了解
- 基于nextcloud构建个人网盘
- Qu'est - ce que le codage par titre?
- 【无标题】
- 452. Detonate the balloon with the minimum number of arrows
- Mathematical function of MySQL function summary
- Program design of dot matrix Chinese character display of basic 51 single chip microcomputer
- It is suggested that Siyuan notes can be compatible with third-party sync disks
- day18
- R语言实战应用案例:绘图篇(三)-多种组合图型绘制
猜你喜欢

Palindrome related topics

头部姿态估计原理及可视化_loveliuzz的博客-程序员宅基地_头部姿态估计

Typora图床配置详细教程

Live classroom system 03 supplement model class and entity

LZ77 file compression

leetcode: 17. 电话号码的字母组合

Official wechat product! Applet automation framework minium sharing Preview

C# 线程锁和单多线程简单使用
![[test platform development] XVII. The interface editing page realizes the drop-down cascade selection, and binds the module to which the interface belongs](/img/19/de8f4de5adbfcfb62291b8664187fd.png)
[test platform development] XVII. The interface editing page realizes the drop-down cascade selection, and binds the module to which the interface belongs

基本51单片机点阵汉字显示程序设计
随机推荐
【数组&&字符串&&宏练习题】
Towhee 每周模型
The accuracy of digital addition
[applet automation minium] III. element positioning - use of wxss selector
CSDN写文方法(二)
C# 线程锁和单多线程简单使用
[test platform development] 21. complete sending interface request and display response header information
@FeignClient使用詳細教程(圖解)
Sword finger offer19 regular expression
一道代码题看 CommonJS 模块化规范
Common JS modular specification from a code question
【无标题】
【软件测试】如何梳理你测试的业务
452. Detonate the balloon with the minimum number of arrows
正则表达式常用语法解析
[test platform development] XVII. The interface editing page realizes the drop-down cascade selection, and binds the module to which the interface belongs
中望CAD专业版 2022软件安装包下载及安装教程
String function of MySQL function summary
真人踩过的坑,告诉你避免自动化测试常犯的10个错误
[untitled] test [untitled] test