Promise实现
整体代码
(function (window) {
function Promise(executor) {
const self = this
self.PromiseState = 'pending'
self.PromiseResult = null
self.callbacks = []
function resolve(value) {
if(self.PromiseState !== 'pending') {
return
}
self.PromiseState = 'fulfilled'
self.PromiseResult = value
if (self.callbacks.length>0) {
setTimeout(() => {
self.callbacks.forEach(callbacksObj => {
callbacksObj.onResolved(value)
})
});
}
}
function reject(reason) {
if(self.PromiseState !== 'pending') {
return
}
self.PromiseState = 'rejected'
self.PromiseResult = reason
if (self.callbacks.length>0) {
setTimeout(() => {
self.callbacks.forEach(callbacksObj => {
callbacksObj.onRejected(reason)
})
});
}
}
try {
executor(resolve, reject)
} catch (error) {
reject(error)
}
}
Promise.prototype.then = function (onResolved, onRejected) {
const self =this;
if(typeof onRejected !== 'function'){
onRejected = reason => {
throw reason;
}
}
if(typeof onResolved !== 'function'){
onResolved = value => value;
}
return new Promise((resolve,reject) => {
function callback(type){
try{
let result = type(self.PromiseResult);
if(result instanceof Promise){
result.then(
v => {
resolve(v);
},
r => {
reject(r);
}
)
}else{
resolve(result);
}
}catch(e){
reject(e);
}
}
if(this.PromiseState === 'fulfilled'){
setTimeout(() => {
callback(onResolved)
})
}
if(this.PromiseState === 'rejected'){
setTimeout(() => {
callback(onRejected)
})
}
if(this.PromiseState === 'pending'){
this.callbacks.push({
onResolved: function() {
callback(onResolved)
},
onRejected: function() {
callback(onRejected)
}
});
}
})
}
Promise.prototype.catch = function (onRejected) {
return this.then(undefined,onRejected);
}
Promise.resolve = function (value) {
return new Promise((resolve,reject) => {
if(value instanceof Promise){
value.then(v=>{
resolve(v);
},r=>{
reject(r)
})
}else{
resolve(value)
}
})
}
Promise.rejected = function (reason) {
return new Promise((resolve,reject) => {
reject(reason)
})
}
Promise.all = function (promises) {
return new Promise((resolve,reject)=>{
let count=0;
let arr=[];
for(let i=0;i<promises.length;i++){
promises[i].then(v=>{
count++;
arr[i]=v;
if(count === promises.length){
resolve(arr);
}
},
r=>{
reject(r);
})
}
})
}
Promise.race = function (promises) {
return new Promise((resolve,reject) => {
for(let i=0;i<promises.length;i++){
promises[i].then(v=>{
resolve(v);
},r=>{
reject(r);
})
}
})
}
window.Promise = Promise
})(window)
executor:执行器函数(同步执行)
function Promise(executor) {
const self = this
self.PromiseState = 'pending'
self.PromiseResult = null
self.callbacks = []
function resolve(value) {
if(self.PromiseState !== 'pending') {
return
}
self.PromiseState = 'fulfilled'
self.PromiseResult = value
if (self.callbacks.length>0) {
setTimeout(() => {
self.callbacks.forEach(callbacksObj => {
callbacksObj.onResolved(value)
})
});
}
}
function reject(reason) {
if(self.PromiseState !== 'pending') {
return
}
self.PromiseState = 'rejected'
self.PromiseResult = reason
if (self.callbacks.length>0) {
setTimeout(() => {
self.callbacks.forEach(callbacksObj => {
callbacksObj.onRejected(reason)
})
});
}
}
try {
executor(resolve, reject)
} catch (error) {
reject(error)
}
}
then方法实现
Promise.prototype.then = function (onResolved, onRejected) {
const self =this;
if(typeof onRejected !== 'function'){
onRejected = reason => {
throw reason;
}
}
if(typeof onResolved !== 'function'){
onResolved = value => value;
}
return new Promise((resolve,reject) => {
function callback(type){
try{
let result = type(self.PromiseResult);
if(result instanceof Promise){
result.then(
v => {
resolve(v);
},
r => {
reject(r);
}
)
}else{
resolve(result);
}
}catch(e){
reject(e);
}
}
if(this.PromiseState === 'fulfilled'){
setTimeout(() => {
callback(onResolved)
})
}
if(this.PromiseState === 'rejected'){
setTimeout(() => {
callback(onRejected)
})
}
if(this.PromiseState === 'pending'){
this.callbacks.push({
onResolved: function() {
callback(onResolved)
},
onRejected: function() {
callback(onRejected)
}
});
}
})
}
catch方法实现
Promise.prototype.catch = function (onRejected) {
return this.then(undefined,onRejected);
}
resolve方法实现
Promise.resolve = function (value) {
return new Promise((resolve,reject) => {
if(value instanceof Promise){
value.then(v=>{
resolve(v);
},r=>{
reject(r)
})
}else{
resolve(value)
}
})
}
reject方法实现
Promise.rejected = function (reason) {
return new Promise((resolve,reject) => {
reject(reason)
})
}
all方法实现
Promise.all = function (promises) {
return new Promise((resolve,reject)=>{
let count=0;
let arr=[];
for(let i=0;i<promises.length;i++){
promises[i].then(v=>{
count++;
arr[i]=v;
if(count === promises.length){
resolve(arr);
}
},
r=>{
reject(r);
})
}
})
}
race方法实现
Promise.race = function (promises) {
return new Promise((resolve,reject) => {
for(let i=0;i<promises.length;i++){
promises[i].then(v=>{
resolve(v);
},r=>{
reject(r);
})
}
})
}
|