const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'
class MPromise {
FULFILLED_CALLBACK_LIST = []
REJECTED_CALLBACK_LIST = []
_status = PENDING
constructor(fn) {
this.status = PENDING
this.value = null
this.reason = null
try {
fn(this.resolve.bind(this), this.reject.bind(this))
} catch (e) {
this.reject(e)
}
}
get status() {
return this._status
}
set status(newStatus) {
this._status = newStatus
switch (newStatus) {
case FULFILLED: {
this.FULFILLED_CALLBACK_LIST.forEach((callback) => {
callback(this.value)
})
break
}
case REJECTED: {
this.REJECTED_CALLBACK_LIST.forEach((callback) => {
callback(this.reason)
})
break
}
}
}
resolve(value) {
if (this.status === PENDING) {
this.value = value
this.status = FULFILLED
}
}
reject(reason) {
if (this.status === PENDING) {
this.reason = reason
this.status = REJECTED
}
}
then(onFulfilled, onRejected) {
const realOnFulfilled = this.isFunction(onFulfilled)
? onFulfilled
: (value) => {
return value
}
const realOnRejected = this.isFunction(onRejected)
? onRejected
: (reason) => {
throw reason
}
const promise2 = new MPromise((resolve, reject) => {
const fulfilledMicrotask = () => {
queueMicrotask(() => {
try {
const x = realOnFulfilled(this.value)
this.resolvePromise(promise2, x, resolve, reject)
} catch (e) {
reject(e)
}
})
}
const rejectedMicrotask = () => {
queueMicrotask(() => {
try {
const x = realOnRejected(this.reason)
this.resolvePromise(promise2, x, resolve, reject)
} catch (e) {
reject(e)
}
})
}
switch (this.status) {
case FULFILLED: {
fulfilledMicrotask()
break
}
case REJECTED: {
rejectedMicrotask()
break
}
case PENDING: {
this.FULFILLED_CALLBACK_LIST.push(fulfilledMicrotask)
this.REJECTED_CALLBACK_LIST.push(rejectedMicrotask)
}
}
})
return promise2
}
catch(onRejected) {
return this.then(null, onRejected)
}
isFunction(param) {
return typeof param === 'function'
}
resolvePromise(promise2, x, resolve, reject) {
if (promise2 === x) {
return reject(
new TypeError('The promise and the return value are the same')
)
}
if (x instanceof MPromise) {
queueMicrotask(() => {
x.then((y) => {
this.resolvePromise(promise2, y, resolve, reject)
}, reject)
})
} else if (typeof x === 'object' || this.isFunction(x)) {
if (x === null) {
return resolve(x)
}
let then = null
try {
then = x.then
} catch (error) {
return reject(error)
}
if (this.isFunction(then)) {
let called = false
try {
then.call(
x,
(y) => {
if (called) return
called = true
this.resolvePromise(promise2, y, resolve, reject)
},
(r) => {
if (called) return
called = true
reject(r)
}
)
} catch (error) {
if (called) return
reject(error)
}
} else {
resolve(x)
}
} else {
resolve(x)
}
}
static race(promiseList) {
return new MPromise((resolve, reject) => {
const length = promiseList.length
if (length === 0) {
return resolve()
} else {
for (let i = 0; i < length; i++) {
MPromise.resolve(promiseList[i]).then(
(value) => {
return resolve(value)
},
(reason) => {
return reject(reason)
}
)
}
}
})
}
static all(promiseList) {
return new MPromise( (resolve, reject) => {
const length = promiseList.length
let flag = false
let count = 0;
let promise_resolve_result = []
let promise_reject_result = null
if (length === 0) {
return resolve()
} else {
for (let i = 0; i < length; i++) {
MPromise.resolve(promiseList[i])
.then(
(value) => {
promise_resolve_result[i] = value
count++;
if(count === length - 1) {
resolve(promise_resolve_result)
}
},
)
.catch(reason => {
reject(reason)
})
}
}
})
}
static allSettled(promiseList) {
return new MPromise((resolve, reject) => {
if (!Array.isArray(promiseList)) {
return reject(new TypeError('参数必须是一个数组'))
}
const length = promiseList.length
let flag = false
let count = 0;
let resolvedArray = []
if (length === 0) {
return resolve()
} else {
for (let i = 0; i < length; i++) {
MPromise.resolve(promiseList[i])
.then((value) => {
resolvedArray[i] = {
status: 'fulfilled',
value,
}
})
.catch((reason) => {
resolvedArray[i] = {
status: 'rejected',
reason,
}
})
.finally(() => {
count++
if (count === promiseNum) {
resolve(resolvedArray)
}
})
}
}
})
}
static resolve(value) {
if (value instanceof MPromise) {
return value
}
return new MPromise((resolve) => {
resolve(value)
})
}
static reject(reason) {
return new MPromise((resolve, reject) => {
reject(reason)
})
}
}
const test = new MPromise((resolve, reject) => {
setTimeout(() => {
reject(111)
}, 1000)
})
.then((value) => {
console.log('then')
})
.catch((reason) => {
console.log('catch')
})