网站首页 > 博客文章 正文
1:基本概念
从字面意思上来看,Promise是承诺。它表示一个异步操作的最终状态(完成或失败),以及该异步操作的结果值。根据Pormise的执行结果可以拿到一个最终状态。
new Promise({ function(resolve,reject){ /* executor */ } });
一般来讲,有三种状态:
- pending,进行中
- fulfilled,已成功
- rejected,已失败
关于三种状态,实际上分为两种情况:一种是从pending到fulfilled,也就是执行然后成功;另一种pending到rejected是执行,然后失败。Promise 的状态一旦确定就不再改变。只能够从pending到fulfill或者是从pending到rejected,确定之后的状态称之为resolved。resolved之后添加回调会立即执行得到结果,resolved一般指的是fulfilled状态。
一旦新建会立即执行,无法中途取消。Promise构造函数执行时立即调用executor函数。
基本用法
- Promise接收一个函数作为参数,该函数的参数分别是resolved和reject
let promise = new Promise(function(resolve,reject){ console.log('new promise'); setTimeout(function(){ console.log('promise resolved'); return resolved('success'); },1000); });
- Promise实例生成后在then方法中指定resolved状态和rejected(可选)状态的回调函数
promise.then(function(value){ console.log('resolved callback',value); });
- Promise传递
const promise1 = new Promise(function (resolve,reject)){ /* .... */ }
属性和方法
- Promise.prototype.then,是Promise状态改变时的回调函数,也就是定义了一个承诺,那么得到的结果是什么。它返回一个新的Promise,新的Promise意味着可以链式调用,也就是.then之后还可以.then 。
// 顺次调用 let promise1 = new Promise(function(resolve, reject) { console.log('new promise1'); setTimeout(function() { console.log('promise1 resolved'); return resolve("promise1 success"); }, 2000); }); let promise2 = new Promise(function(resolve, reject) { console.log('new promise2'); setTimeout(function() { console.log('promise2 resolved'); return resolve("promise2 success"); }, 4000); }); promise1 .then(function(value) { console.log('then1 callback', value); return value; }) .then(function(value) { console.log('then2 callback', value); return promise2; }) .then(function(value) { console.log('then3 callback', value); return ''; })
- Promise.prototype.catch(错误捕获)
- 错误发生时的回调函数,相当于.then(null/undefined,reject)的一个别名,它捕获reject以及then回调运行时的错误。
// 顺次调用 let promise = new Promise(function(resolve, reject) { throw new Error('Error'); }); promise.catch(function(error) { console.log(error); }); // Error: Error
- Promise.prototype.finally(最后)
- 不管成功或失败,这个方法无论状态如何都会执行。它不接受任何参数,与状态无关,不依赖Promise执行结果。
- Promise.all(所有)
- 把所有的Promise放在一起,然后当所有的Promise全部resolved,这个Promise.all才会真正的resolved。也就是所有的成功了它才成功,可以理解成一个包装器,它包装多个Promise为一个新的Promise,那么Promise.all实际上接收的是一个数组,fulfilled条件是所有包装的Promise全部fulfilled。
const promise1 = new Promise((resolve, reject) => { resolve({ code: 200, data: [], }); }) .then(result => result) .catch(err => ({ code: 200, data: [], })); const promise2 = new Promise((resolve, reject) => { throw new Error('Error'); }) .then(result => result) .catch(err => ({ code: 200, data: [], })); Promise.all([promise1, promise2]) .then(result => console.log(result)) .catch(e => console.log(e)); ``` 使用Promise.all,我们需要给每一个Promise都加一个catch,返回一个备用的结果,这个月整个的Promise.all才会得到一个resolved结果。
- Promise.race(谁先返回就先执行谁)
包含多个Promise为一个新的Promise,返回第一个fulfilled。 const promise = Promise.race([ fetch('/url'), new Promise(function (resolve, reject) { setTimeout(() => { return reject(new Error('request timeout')); }, 5000); }) ]); p .then(console.log) .catch(console.error);
- Promise.resolve(状态)
- 将现有对象转为Promise对象,状态为fulfilled。
const promise = Promise.resolve('hello'); promise.then(res => alert(res));
- Promise.reject
- 将现有对象转为Promise对象,状态为rejected。
const promise = Promise.reject('word'); promise.then(res => alert(res)).catch(err => alert(err));
猜你喜欢
- 2024-10-27 前端面试大全:手写 Promise(前端手机端面试题)
- 2024-10-27 Javascript ES6中 Generator的?async/await Promise 了解一下?
- 2024-10-27 Promise 中 race 方法的目的是什么
- 2024-10-27 ES6 Promise对象(es6对象操作)
- 2024-10-27 ES6 Promise 的最佳实践(es6 promise作用)
- 2024-10-27 ES6学习(17):彻底搞懂 async 和 await,轻松掌握异步编程!
- 2024-10-27 自己造轮子,超详细、简单的Promise对象原理讲解及代码实现
- 2024-10-27 JavaScript ES6 语法特性介绍(javascript的基本语法遵循的标准是____。)
- 2024-10-27 图解 Promise 实现原理(一):基础实现
- 2024-10-27 可视化的 js:动态图演示 Promises & Async/Await 的过程
你 发表评论:
欢迎- 06-23MySQL合集-mysql5.7及mysql8的一些特性
- 06-23MySQL CREATE TABLE 简单设计模板交流
- 06-23MYSQL表设计规范(mysql设计表注意事项)
- 06-23MySQL数据库入门(四)数据类型简介
- 06-23数据丢失?别慌!MySQL备份恢复攻略
- 06-23MySQL设计规范(mysql 设计)
- 06-23MySQL数据实时增量同步到Elasticsearch
- 06-23MySQL 避坑指南之隐式数据类型转换
- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)