class mypromise {
constructor(callback){
this.state = 'pending'
this.fulfilledcallback;
this.rejectcallback;
this.res=null;
this.resolve = (res) =>{
this.state = 'fulfilled'
this.res = res;
if(this.fulfilledcallback){
this.fulfilledcallback(res)
}
return this;
}
this.reject = (res) =>{
this.state = 'rejected'
this.res = res
if(this.rejectcallback){
this.rejectcallback(res)
}
return this;
}
this.then = (callback) =>{
this.fulfilledcallback = callback
if(this.state === 'fulfilled'){
this.fulfilledcallback(this.res)
}
return this;
}
this.catch = (callback) =>{
this.rejectcallback = callback;
if(this.state === 'rejected'){
this.rejectcallback(this.res)
}
return this;
}
callback(this.resolve, this.reject)
}
}
mypromise.all = (arr) => {
this.state = 'pending'
this.num = arr.length
this.fulfilled = 0
this.reject = 0
this.res = []
this.fulfilledcallback
this.rejectedcallback
this.then = (callback) =>{
this.fulfilledcallback = callback;
if(this.state === 'fulfilled'){
this.fulfilledcallback(this.res)
}
return this
}
this.catch= (callback) =>{
this.rejectedcallback = callback;
if(this.state === 'rejected'){
this.rejectedcallback(this.res)
}
return this
}
arr.forEach((value)=>{
value.then((res)=>{
this.res.push(res)
this.fulfilled++
if(this.fulfilled === this.num){
this.state = 'fulfilled';
if(this.fulfilledcallback){
this.fulfilledcallback(this.res)
}
}
}).catch((res)=>{
this.res.push(res)
this.reject++
if(this.state != 'rejected'){
this.state = 'rejected';
if(this.rejectedcallback){
this.rejectedcallback(this.res);
}
}
})
})
return this;
}
mypromise.race = (arr) => {
this.state = 'pending'
this.num = arr.length
this.fulfilled = 0
this.reject = 0
this.res = []
this.fulfilledcallback
this.rejectedcallback
this.then = (callback) =>{
this.fulfilledcallback = callback;
if(this.state === 'fulfilled'){
this.fulfilledcallback(this.res)
}
return this
}
this.catch= (callback) =>{
this.rejectedcallback = callback;
if(this.state === 'rejected'){
this.rejectedcallback(this.res)
}
return this
}
arr.forEach((value)=>{
value.then((res)=>{
this.res.push(res)
this.fulfilled++
if(this.state === 'pending'){
this.state = 'fulfilled';
if(this.fulfilledcallback){
this.fulfilledcallback(this.res)
}
}
}).catch((res)=>{
this.res.push(res)
this.reject++
if(this.state === 'pending'){
this.state = 'rejected';
if(this.rejectedcallback){
this.rejectedcallback(this.res);
}
}
})
})
return this;
}
mypromise.allSettled = (arr) => {
this.state = 'pending'
this.num = arr.length
this.fulfilled = 0
this.reject = 0
this.res = []
this.fulfilledcallback
this.rejectedcallback
this.then = (callback) =>{
this.fulfilledcallback = callback;
if(this.state === 'fulfilled'){
this.fulfilledcallback(this.res)
}
return this
}
this.catch= (callback) =>{
this.rejectedcallback = callback;
if(this.state === 'rejected'){
this.rejectedcallback(this.res)
}
return this
}
arr.forEach((value)=>{
value.then((res)=>{
this.res.push(res)
this.fulfilled++
if(this.fulfilled === this.num){
this.state = 'fulfilled';
if(this.fulfilledcallback){
this.fulfilledcallback(this.res)
}
}
}).catch((res)=>{
this.res.push(res)
this.fulfilled++
if(this.fulfilled === this.num){
this.state = 'fulfilled';
if(this.fulfilledcallback){
this.fulfilledcallback(this.res)
}
}
})
})
return this;
}
mypromise.any = (arr) => {
this.state = 'pending'
this.num = arr.length
this.fulfilled = 0
this.reject = 0
this.res = []
this.fulfilledcallback
this.rejectedcallback
this.then = (callback) =>{
this.fulfilledcallback = callback;
if(this.state === 'fulfilled'){
this.fulfilledcallback(this.res)
}
return this
}
this.catch= (callback) =>{
this.rejectedcallback = callback;
if(this.state === 'rejected'){
this.rejectedcallback(this.res)
}
return this
}
arr.forEach((value)=>{
value.then((res)=>{
this.res.push(res)
this.fulfilled++
if(this.state != 'fulfilled'){
this.state = 'fulfilled';
if(this.fulfilledcallback){
this.fulfilledcallback(this.res)
}
}
}).catch((res)=>{
this.res.push(res)
this.reject++
if(this.reject === this.num){
this.state = 'rejected';
if(this.rejectedcallback){
this.rejectedcallback(this.res);
}
}
})
})
return this;
}
window.mypromise = mypromise