//构造函数的参数是一个异步任务
function Promise(task) {
let that = this;//缓存this
//默认状态为pending
that.status = 'pending';
//此变量里放着此promise的结果
that.value = undefined;
//存放的着所有成功的回调函数
that.onResolvedCallbacks = [];
//存放着所有的失败的回调函数
that.onRejectedCallbacks = [];
//调用此方法可以把promise变成成功态
//resolve的时候你把挣到的钱传进来
function resolve(value) {
if(value instanceof Promise){
return value.then(resolve,reject);
}
if (that.status == 'pending') {
that.status = 'fulfilled';
that.value = value;
that.onResolvedCallbacks.forEach(item=>item(that.value));
}
}
//调用此方法可以把当前的promise变成失败态
function reject(reason) {
//如果当前状态是初始态,则转成失败态
if (that.status == 'pending') {
that.status = 'rejected';
that.value = reason;
that.onRejectedCallbacks.forEach(item=>item(that.value));
}
}
//立即执行传入的任务
try {
task(resolve, reject);
} catch (e) {
reject(e);
}
}
function resolvePromise(promise2,x,resolve,reject){
let then;
//如果x就是promise2
if(promise2 === x){
return reject(new TypeError('循环引用'));
}
if(x instanceof Promise){
if(x.status == 'pending'){
x.then(function(y){
resolvePromise(promise2,y,resolve,reject);
},reject);
}else if(x.status == 'fulfilled'){
resolve(x.value);
}else if(x.status == 'rejected'){
reject(x.value);
}
}else if(x!=null && (typeof x == 'object' || typeof x == 'function')){
try{
then = x.then;
if(typeof then == 'function'){
then.call(x,function(y){
resolvePromise(promise2,y,resolve,reject)
},reject);
}
}catch(e){
reject(e);
};
}else{
resolve(x);
}
}
//onFulfilled成功的回调,onReject失败的回调
Promise.prototype.then = function (onFulfilled, onReject) {
onFulfilled = typeof onFulfilled == 'function'?onFulfilled:function(value){return value};
onReject = typeof onReject=='function'?onReject:function(reason){
throw reason;
}
let that = this;
let promise2;
if(that.status == 'fulfilled'){
promise2 = new Promise(function(resolve,reject){
let x = onFulfilled(that.value);
resolvePromise(promise2,x,resolve,reject);
});
}
if(that.status == 'rejected'){
promise2 = new Promise(function(resolve,reject){
let x = onReject(that.value);
resolvePromise(promise2,x,resolve,reject);
});
}
if(that.status == 'pending'){
promise2 = new Promise(function(resolve,reject){
that.onResolvedCallbacks.push(function(){
let x = onFulfilled(that.value);
resolvePromise(promise2,x,resolve,reject);
});
that.onRejectedCallbacks.push(function(){
let x = onReject(that.value);
resolvePromise(promise2,x,resolve,reject);
});
});
}
return promise2;
}
Promise.all = function (arr) {
return new Promise((resolve,reject) =>{
let values = []
let len = arr.length
for(var i = 0;i < len; i++){
let promise = arr[i]
if(typeof promise.then == 'function'){
promise.then(res=>{
values.push(res)
if(values.length == len){
resolve(values)
}
})
}
}
})
}
Promise.race = function (arr) {
return new Promise((resolve,reject) =>{
let len = arr.length
for(var i = 0;i < len; i++){
let promise = arr[i]
if(typeof promise.then == 'function'){
promise.then(res=>{
resolve(res)
})
}
}
})
}
Promise.resolve = function (value) {
if (value instanceof Promise) return value
return new Promise(resolve => resolve(value))
}
Promise.reject = function (value) {
return new Promise((resolve,reject)=>reject(value))
}
module.exports = Promise;
手写Promise
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前言 JSONP以前研究过,最近又有点忘了,写篇本文mark一下,旨在理解记住JSONP的原理及其实现。代码实现用...
- 问题一 在系列(一)的开头,我曾经举过一个例子,但其中包含着错误(现已更正),现简化如下: 原因:之所以会出现这种...
- 作者介绍:牛月月 微信公众号:她的旅途(girl_travelling) 程序媛一枚,用镜头记录美好,用笔尖书写奇...