手写一个promise
Promise A+ 规范:https://promisesaplus.com/
注:以下代码没有通过 promises-aplus-tests 的全部测试,但基本功能还是全的( 测试结果: 864 passing, 8 failing)
另外可以参考这个指南中的 Promise实现
代码实现
// Promise resolve_100+ 规范:https://promisesaplus.com/
class myPromise {
constructor(executor) {
this.state = 'pending';
this.value = undefined;
this.reason = undefined;
this.onResolvedArray = [];
this.onRejectedArray = [];
const resolve = (val) => {
if (this.state === "pending") {
this.state = 'resolved';
this.value = val;
this._doResolve();
}
};
const reject = (rejectReason) => {
if (this.state === "pending") {
this.state = 'rejected';
this.reason = rejectReason;
this._doReject();
}
};
try {
executor(resolve, reject);
} catch (e) {
reject(e);
}
}
then(onResolved, onRejected) {
let promise2;
promise2 = new myPromise((resolve, reject) => {
// 遵循 2.2
const afterResolved = value => {
try {
typeof onResolved === "function"
? resolvePromise(promise2, onResolved(value), resolve, reject)
: resolvePromise(promise2, value, resolve, reject);
} catch (e) {
reject(e);
}
};
const afterRejected = reason => {
try {
typeof onRejected === "function"
? resolvePromise(promise2, onRejected(reason), resolve, reject)
: reject(reason);
} catch (e) {
reject(e);
}
};
// 2.1
switch (this.state) {
case "pending":
this.onResolvedArray.push(afterResolved);
this.onRejectedArray.push(afterRejected);
break;
case "resolved":
this.onResolvedArray.push(afterResolved);
this._doResolve();
break;
case "rejected":
this.onRejectedArray.push(afterRejected);
this._doReject();
break;
}
});
return promise2;
}
// 执行所有的 onResolved
_doResolve() {
// XXX: 模拟一下microTask
Promise.resolve().then(() => {
this.onResolvedArray.forEach(f => f(this.value));
this.onResolvedArray = [];
});
}
// 执行所有的 onRejected
_doReject() {
// XXX: 模拟一下microTask
Promise.resolve().then(() => {
// if(this.onRejectedArray.length ===0) console.error('Uncaught myPromise', this.reason && this.reason.message || this.reason);
this.onRejectedArray.forEach(f => f(this.reason));
this.onRejectedArray = [];
});
}
// then(null, onRejected) 的别名
catch(onRejected) {
return this.then(null, onRejected);
}
static resolve(val) {
return new myPromise((resolve) => {
resolve(val);
});
}
static reject(reason) {
return new myPromise((resolve, reject) => {
reject(reason);
});
}
static all(promiseList) {
return new myPromise((resolve, reject) => {
const result = [];
let count = 0;
promiseList.forEach((item, index) => {
item
.then(value => {
count++;
result[index] = value;
if (count === promiseList.length - 1) {
resolve(result);
}
})
.catch(err => {
reject(err);
});
});
});
}
static race(promiseList) {
return new myPromise((resolve, reject) => {
promiseList.forEach(item => {
item.then(resolve, reject);
});
});
}
// 下面两个是为了测试才加的
static deferred() {
let dfd = {};
dfd.promise = new myPromise(function (resolve, reject) {
dfd.resolve = resolve;
dfd.reject = reject;
});
return dfd;
}
static defer() {
let dfd = {};
dfd.promise = new myPromise(function (resolve, reject) {
dfd.resolve = resolve;
dfd.reject = reject;
});
return dfd;
}
}
// 处理onResolve返回promise时的情况
function resolvePromise(promise, x, resolve, reject) {
let called = false;
if (promise === x) {
if (called) return;
called = true;
reject(new TypeError('promise 循环错误'));
console.error('promise 循环错误');
return;
}
try {
if (Object.prototype.toString.call(x) === "[object Object]" || typeof x === "function") {
const then = x.then;
if (typeof then === "function") {
then.call(
x,
value => {
if (called) return;
called = true;
resolvePromise(promise, value, resolve, reject);
},
reason => {
if (called) return;
called = true;
reject(reason);
}
);
} else {
if (called) return;
called = true;
resolve(x);
}
} else {
if (called) return;
called = true;
resolve(x);
}
} catch (e) {
if (called) return;
called = true;
reject(e);
}
}
try {
module.exports = myPromise;
} catch (e) {
}
简单测试
function test(promiseConstructor) {
function log(msg) {
console.log(promiseConstructor.name + '\t\t', msg);
}
const testCase = [
{
["测试同步promise"]() {
let resolve_immediate = promiseConstructor.resolve('immediate');
resolve_immediate.then(value => {
log('resolved', value);
});
let reject_immediate = promiseConstructor.reject('immediate');
reject_immediate.catch(value => {
log('rejected', value);
});
}
},
{
["测试异步promise"]() {
new promiseConstructor((resolve) => {
setTimeout(() => {
resolve(1);
}, 100);
})
.then(val => log('resolve', val));
new promiseConstructor((resolve, reject) => {
setTimeout(() => {
reject(2);
}, 100);
})
.catch(val => log('reject1', val));
}
},
{
["测试链式调用和throw方法"]() {
new promiseConstructor((resolve) => {
setTimeout(() => {
resolve(1);
}, 100);
})
.then(value => {
log(value);
return value + 1;
})
.catch(value => {
log('我不应该出现', value);
return value + 1;
})
.then(value => {
log(value);
throw value + 1;
})
.catch(value => {
log(value);
throw value + 1;
// return value + 1;
})
.then(value => {
log(value);
})
.catch(log);
}
},
{
["测试返回promise"]() {
new promiseConstructor((resolve) => {
setTimeout(() => {
resolve(1);
}, 100);
})
.then(value => {
return new promiseConstructor((resolve) => {
setTimeout(() => {
resolve(value + 1);
resolve(value + 1); // 这里重复调用了resolve,但是并不会有什么后果
});
});
})
.then(value => {
log(value);
return value + 1;
})
.then(value => {
return new promiseConstructor((resolve, reject) => {
setTimeout(() => {
reject(value + 1);
resolve(value + 1); // 这里重复调用了resolve,但是并不会有什么后果
});
});
})
.catch(value => {
log(value);
return value + 1;
});
}
},
{
["测试promise.all"]() {
promiseConstructor.all([
promiseConstructor.resolve(1),
promiseConstructor.resolve(2)
])
.then(log)
.catch((err) => {
log(err, '我不出现');
});
promiseConstructor.all([
promiseConstructor.reject(1),
promiseConstructor.resolve('我不出现1'),
promiseConstructor.reject('我不出现2'),
])
.then((err) => {
log(err, '我不出现');
})
.catch(log);
}
},
{
["测试promise.race"]() {
promiseConstructor.race([
new promiseConstructor(resolve => {
setTimeout(() => {
resolve('我不出现');
}, 200);
}),
new promiseConstructor(resolve => {
setTimeout(() => {
resolve(1);
}, 100);
})
])
.then(log)
.catch((err) => {
log(err, '我不出现');
});
promiseConstructor.race([
promiseConstructor.reject(1),
promiseConstructor.resolve('我不出现1'),
promiseConstructor.reject('我不出现2'),
])
.then((err) => {
log(err, '我不出现');
})
.catch(log);
}
},
{
["测试循环promise"]() {
let a = new promiseConstructor(resolve => {
setTimeout(() => {
resolve(1);
}, 100);
})
.then(value => {
return 2;
})
.then(value => {
log(value, '应该报循环引用错误2');
return a;
});
// 不知道为什么,这里如果加一个.catch或者.then就不会报错了
// 原生的promise也是这样
}
}
];
for (let i = 0, len = testCase.length; i < len; i++) {
const item = testCase[i];
setTimeout(() => {
const name = Object.keys(item)[0];
console.group(name);
item[name]();
setTimeout(() => {
console.groupEnd();
}, 900);
}, i * 1000);
}
}
test(myPromise);
test(Promise);
手写一个promise的更多相关文章
- 【原】手写一个promise
上一篇文章中,我们介绍了Promise的基本使用,在这篇文章中,我们试着自己来写一个Promise,主要是学习Promise的内部机制,学习它的编程思想. !!!备注:本文写的不好,仅供自己学习之用, ...
- 掘金转载-手写一个Promise
目录 一 什么是Promise ? 二 Promises/A+ 规范 2.1 术语 2.2 基本要求 2.2.1. Promise的状态 2.2.2. Then 方法 2.3 简易版实践 2.4 进一 ...
- 手写一个Promise/A+,完美通过官方872个测试用例
前段时间我用两篇文章深入讲解了异步的概念和Event Loop的底层原理,然后还讲了一种自己实现异步的发布订阅模式: setTimeout和setImmediate到底谁先执行,本文让你彻底理解Eve ...
- 面试----你可以手写一个promise吗
参考:https://www.jianshu.com/p/473cd754311f <!DOCTYPE html> <html> <head> <meta c ...
- 只会用就out了,手写一个符合规范的Promise
Promise是什么 所谓Promise,简单说就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作)的结果.从语法上说,Promise 是一个对象,从它可以获取异步操作的消息.Prom ...
- Javascript之我也来手写一下Promise
Promise太重要了,可以说是改变了JavaScript开发体验重要内容之一.而Promise也可以说是现代Javascript中极为重要的核心概念,所以理解Promise/A+规范,理解Promi ...
- 『练手』手写一个独立Json算法 JsonHelper
背景: > 一直使用 Newtonsoft.Json.dll 也算挺稳定的. > 但这个框架也挺闹心的: > 1.影响编译失败:https://www.cnblogs.com/zih ...
- 教你如何使用Java手写一个基于链表的队列
在上一篇博客[教你如何使用Java手写一个基于数组的队列]中已经介绍了队列,以及Java语言中对队列的实现,对队列不是很了解的可以我上一篇文章.那么,现在就直接进入主题吧. 这篇博客主要讲解的是如何使 ...
- 【spring】-- 手写一个最简单的IOC框架
1.什么是springIOC IOC就是把每一个bean(实体类)与bean(实体了)之间的关系交给第三方容器进行管理. 如果我们手写一个最最简单的IOC,最终效果是怎样呢? xml配置: <b ...
随机推荐
- jenkins+testlink+python搭建自动化测试环境
一. 环境搭建 jenkins安装与配置请参考我的另一篇博文:https://www.cnblogs.com/wuxunyan/p/9592953.html testlink安装请参考博文:https ...
- 网络教程(7)OSI模型的低层模型
OSI Model——Open System Interconnection Model 开放系统互联模型
- [SDOI2016]生成魔咒(后缀自动机)
看一眼题.本质不同的字串数. 嘴角微微上扬. 每一次加一个数输出一个答案. 笑容渐渐消失. 等等,\(SAM\)好像也可以求本质不同的字串. 设当前字符串用\(x\)表示,每次插入完成后\(ans\) ...
- 【LibreOJ 6279】 数列分块入门 3 (分块)
传送门 code: //By Menteur_Hxy #include<cstdio> #include<iostream> #include<algorithm> ...
- [tyvj-2054][Nescafé29]四叶草魔杖 费用流
lyd讲的最小生成树的题. 道理我都懂,费用流多好写,又好调.但和一般费用流不一样的就是它走过一次后费用需调成0,但是再等回流,就恢复原状即可. #include <queue> #inc ...
- Python中图像的缩放 resize()函数的应用
cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) -> dst 参数说明: src - 原图 dst - 目标图像.当参数ds ...
- Memcache Redis 与Mogodb优缺点
MemcachedMemcached的优点: Memcached可以利用多核优势,单实例吞吐量极高,可以达到几十万QPS(取决于key.value的字节大小以及服务器硬件性能,日常环境中QPS高峰大约 ...
- [ASP.NET]EF选一个空表的情况
public List<DAL.StuFillAnswers> FillsToCheck(int sid,int eid) { using (DAL.ExamSysEntities db ...
- xhprof安装&&使用
听同事说起过一个php性能分析扩展,叫xhprof,近期了解了下. XHProf 是一个轻量级的分层性能測量分析器. 在数据收集阶段.它跟踪调用次数与測量数据,展示程序动态调用的弧线图. 它在报告.后 ...
- 最大似然估计的缺陷 —— 方差和均值的 bias
0. 均匀分布期望的最大似然估计 首先我们来看,如何通过最大似然估计的形式估计均匀分布的期望.均匀分布的概率密度函数为:f(x|θ)=1θ,0≤x≤θ.不失一般性地,将 x1,x2,-,xn 排序为顺 ...