道可道,非常道——详解promise
promise 出来已久,以前一直使用,没有仔细剖析原理,最近在复习es6的知识,写一下自己对于promise的理解。
promise是es6的一种异步编程解决方案,避免频繁的回调函数,增强代码的可阅读性。
写法很简单:
let p2 = new Promise((reslove, reject) => {
reslove(1)
})
console.log(p2.then(res => {
alert(res)
}))
Promise是内置的构造函数,reslove、reject是固定的,只能这么写。reslove()表示成功,reject()表示失败。
方法:
- then() then是Promise的核心方法,是用来获取异步的返回结果。有两个参数用来获取reslove 或 reject 的值。
- catch() 用来捕获异常。
- all() 多个promise,所有的参与的promise对象全部执行完才会出结果。参数为类数组对象。
- race() 参数为类数组对象,任何一个参与的promise对象执行完,就会出结果。
以上为常用的四个方法。
代码:
/*
*
* promise
* 异步编程的一种解决方案。
*
* */
const promise = new Promise(function (reslove, reject) {
console.log('开始')
setTimeout(function () {
reslove(2)
}, 3000)
}) promise.then(function (value) {
return 3;
}).then(function (value) { }) //
let p = new Promise((reslove, reject) => {
setTimeout(()=>{
let a = Math.random();
if (a>0.5) {
//reslove(a); // 这里说白了是就是调用下面的then中的函数 a
reslove.call(null, a) // 这样也行
} else { reject(a) // 调用b
}
reject(a) // 调用b
}, 1000)
}) p.then(res => { // 假设这是函数a
return res;
}, rej => { // 函数b
return rej;
}).then(success => { // 链式调用的重点是前边的then必须return 否则 为undefined
return success;
}, fail => {
return fail;
}) // then() then方法是挂在Promise.prototype上的
console.log(Promise.prototype) // Promise {constructor: ƒ, then: ƒ, catch: ƒ, finally: ƒ, Symbol(Symbol.toStringTag): "Promise"} // catch() 相当于 then(null, reject)
p.then(rel => {
console.log(rel)
return rel;
},rej => {
console.log(rej + '小')
return rej;
}).then(rel => { // 前一个then返回成功或失败都在这里 因为执行catch()后返回一个新的实例该实例该实例执行完之后会变为reslove ,因此后边的catch始终执行不到
console.log(rel)
alert(2)
}).catch(rej => { // 这句是走不到的
console.log(rej)
alert(1)
console.log('二小')
}) // finally() 状态成功或失败 都会执行此操作
let p1 = new Promise((reslove, reject) => {
console.log(this) // window 箭头函数本身没有this 因此this指向定义箭头函数时的上下文
reslove(1);
}) p1.then(res => {
console.log('success')
}).finally(res => {
console.log('finally')
}) // all() 所有实例的状态都改变完才改变
let p2 = new Promise((reslove, reject) => {
setTimeout(() => {
alert(1)
reslove(1)
} ,1000)
}) let p3 = new Promise((reslove, reject) => {
setTimeout(() => {
alert(2)
reslove(2)
} ,2000)
}) let p4 = new Promise((reslove, reject) => {
setTimeout(() => {
alert(3)
reject(3)
} ,3000)
})
Promise.all([p2, p3]).then(res => {
console.log(res) // [1, 2]
}) Promise.all([p2, p3, p4]).then(res => {
console.log(res)
}).catch(rej => {
console.log(rej) // 3 只要有一个reject 就走这里 不走上边的then 返回的永远是第一个reject的promise实例
})
// race()
Promise.race([p2, p3]).then(res => {
console.log(res) // 1
})
下面是手写的实现promise的原理:
// 实现 promise
function MyPromise(fn){ // fn 为 new Promise(fn)
let _this = this;
_this.status = 'pending';
_this.resloveValue = null; // 成功时的数据
_this.rejectValue = null; // 失败时的数据
_this.resloveFnCallbacks = [];
_this.rejectFnCallbacks = []; function reslove(value) {
if (_this.status == 'pending') {
_this.status = 'resloved';
_this.resloveValue = value;
_this.resloveFnCallbacks.forEach(fn => fn())
}
}
function reject(value) {
if (_this.status == 'pending') {
_this.status = 'rejected';
_this.rejectValue = value;
_this.rejectFnCallbacks.forEach(fn => fn())
} }
// 捕获异常
try{
fn(reslove, reject)
} catch (e) {
reject(e)
} } // then() 应该接受两个参数 成功 或 失败的 回调
MyPromise.prototype.then = function (resloveFn, rejectFn) {
let _this = this;
let promise2; // then 返回的promise // 参数非函数转为函数
resloveFn = typeof resloveFn === 'function'? resloveFn: (function (value) {
return value;
}) rejectFn = typeof rejectFn === 'function'? rejectFn: function (err) {
throw err;
} if(this.status == 'resloved') { // 成功
alert(3)
promise2 = new MyPromise((reslove, reject) => {
setTimeout(() => {
let x = resloveFn(this.resloveValue);
resolvePromise(promise2, x, reslove, reject);
})
})
} if(this.status == 'rejected') { // 失败
rejectFn(this.rejectValue)
} if (this.status == 'pending') {
this.resloveFnCallbacks.push(function(){
resloveFn(_this.resloveValue)
})
this.rejectFnCallbacks.push(function(){
rejectFn(_this.rejectValue)
})
} return promise2; // resolvePromise
function resolvePromise(promise2, x, reslove, reject) {
let called; // 是否调用过 // 判断上一次then返回的值 实质上下一个then只需要一个值便可以,所以我们就是要将上一个的peomise中的值提取出来
// then 返回promise只是为了链式调用,then本身需要一个实际的值,并不需要promise
if (x != null && (typeof x === 'object' || typeof x === 'function')) {
try {
let then = x.then; // 保存x的then方法 这种是x是对象
if (typeof then === 'function') {
then.call(x, function (y) {
if (called) return;
called = true;
resolvePromise(promise2, y, reslove, reject)
}, function (rej) {
if (called) return;
called = true;
reject(rej) })
} else { // x 是普通对象直接返回就行
reslove(x);
}
} catch (e) {
if (called) return;
called = true;
reject(e)
}
} else { // x 是普通值
reslove(x)
}
} } // 将promise
let p6 = new MyPromise(function(reslove, reject) {
reslove(2)
})
p6.then(function(res){
return res;
}).then(function(res){
alert(res)
})
参考链接:https://juejin.im/post/5ab20c58f265da23a228fe0f
道可道,非常道——详解promise的更多相关文章
- “全栈2019”Java第七十三章:外部类里多个静态非静态内部类详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- [GO语言的并发之道] Goroutine调度原理&Channel详解
并发(并行),一直以来都是一个编程语言里的核心主题之一,也是被开发者关注最多的话题:Go语言作为一个出道以来就自带 『高并发』光环的富二代编程语言,它的并发(并行)编程肯定是值得开发者去探究的,而Go ...
- 详解promise、async和await的执行顺序
1.题目和答案 一道题题目:下面这段promise.async和await代码,请问控制台打印的顺序? async function async1(){ console.log('async1 sta ...
- PHP中静态(static)调用非静态方法详解
1.PHP中可以静态调用非静态方法么? 今天我被问到PHP中可不可以使用 className::methodName() 的方法来调用一个没有声明static的方法.在我的印象中,我好像是见过这种用法 ...
- static静态和非静态详解
static 作为Java中的一个关键字,用于修饰方法.成员变量(Field),统称为成员. 有static修饰的成员 属于类 1.方法称为静态方法(类方法),Field称为类的属性. 2.静态成 ...
- PHP中静态(static)调用非静态方法详解--调用!!!
来源:https://www.cnblogs.com/yolo-bean/p/7739265.html 这里分析了php面向对象中static静态属性和静态方法的调用.关于它们的调用(能不能调用,怎么 ...
- Promise和async await详解
本文转载自Promise和async await详解 Promise 状态 pending: 初始状态, 非 fulfilled 或 rejected. fulfilled: 成功的操作. rejec ...
- es6的promise用法详解
es6的promise用法详解 promise 原理 promise是es6的异步编程解决方案, 是es6封装好的对象: 一个promise有三种状态:Pending(进行中).Resolved(已完 ...
- 详解收发不畅原因及U-Mail邮件中继解决之道
邮件在商务往来中扮演着信息交流的重要角色,假如传输受阻,必将造成沟通不畅:可能三五封邮件的投递你意识不到其重要性,但假如长期需和客户保持沟 通,则需要保证其一贯的稳定性,这就很考验相关软件平台的性能是 ...
随机推荐
- PHP的日志记录-错误与异常记录
PHP的日志记录-错误与异常记录 提到 Nginx + PHP 服务的错误日志,我们通常能想到的有 Nginx 的 access 日志.error 日志以及 PHP 的 error 日志.虽然看起来是 ...
- Bower快速学习
什么是bower? Bower是一个前端类库管理器,它可用于搜索.安装和卸载如JavaScript.HTML.CSS之类的类库. 官网:https://bower.io/ 安装bower 使用npm, ...
- 在react中引入下拉刷新和上拉加载
1. 首先引入插件 import ReactPullLoad, {STATS} from 'react-pullload' 2. 初始化: constructor(props) { super(pro ...
- SQL注入之Sqli-labs系列第二篇
废话不在多说 let's go! 继续挑战第二关(Error Based- String) 同样的前奏,就不截图了 ,and 1=1和and 1=2进行测试,出现报错 还原sql语句 查看源代码 ...
- 启动tomcat时jmx port被占用
一.问题描述 今天一来公司,在IntelliJ IDEA 中启动Tomcat服务器时就出现了如下图所示的错误: 错误: 代理抛出异常错误: java.rmi.server.ExportExceptio ...
- 海外仓系统 COD货到付款到付功能
全球还有很多国家买家网购选择货到付款方式,例如东南亚的越南.泰国.印度尼西亚,中东的阿联酋.沙特等国家.在这些国家建立海外仓需要需要具备COD货到付款功能,麦哲伦海外仓系统已经支持COD货到到付结算相 ...
- 极其简单的帮你理解ORM中的关联关系
ORM对象关系映射(英语:(Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序技术,用于实现面向对象编程语言里不同类型系统的数据之间的转 ...
- 阿尔法冲刺——Postmortem会议
设想与目标 1.我们软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 这个问题,我们觉得我们的软件目标还是比较明确的,在SRS中也给出了典型用户和典型场景的清晰的描述. 2 ...
- c语音-第零次作业
1.你认为大学的学习生活.同学关系.师生应该是怎样? 我认为大学学习应该以自我学习为主,由以往的被动学习改为主动学习,探索新世界,除学习专业知识外对自身欠缺的地方也应该加以补足:同学之间要互相帮助,更 ...
- C语言作业(三)
一.完成PTA上四题作业 二.具体解题 (一).A乘以B 1.实验代码 #include <stdio.h> int main() { int A,B,C; scanf("%d ...