class Promise {
callbacks = [];
constructor(fn) {
// 内部的_resolve绑定为外部调用
fn(this._resolve.bind(this))
}
then(onFulfilled){
this.callbacks.push(onFulfilled)
}
_resolve(value){
this.callbacks.forEach(fn => fn(value))
}
}
class Promise {
callbacks = []
state = 'pending'
value = undefined
constructor(fn) {
fn(this._resolve.bind(this)
}
then(onFulfilled = null){
// 下面的resolve就是新的实例的this._resolve
return new Promise(resolve => {
this._handle({ onFulfilled, resolve })
})
}
_handle(callback){
if (this.state === 'pending') {
this.callbacks.push(callback)
return
}
if (!callback.onFulfilled){
// 未设置onFulfilled, resolve 结果给下一个then
callback.resolve(this.value)
return
}
const ret = callback.onFulfilled(this.value)
callback.resolve(ret)
}
_resolve(value) {
// 先切换状态,这样handle里面的判断不会影响
this.state = 'fulfilled'
this.value = value
// 一个实例的所有callbacks执行完
this.callbacks.foreach(callback => this._handle(callback))
}
}
class Promise{
state = 'pending'
callbacks = []
value = undefined
constructor(fn){
fn(this._resolve.bind(this), this._reject.bind(this)
}
_resolve(value){
this.state = 'fullfiled'
this.value = value
this.callbacks.foreach(callback => this._handle(callback))
}
_reject(error){
this.state = 'rejected'
this.value = error
this.callbacks.foreach(callback => this._handle(callback))
}
_handle(callback){
if( this.state === 'pending') {
this.callbacks.push(callback)
return
}
let cb = this.state === 'fullfilled' ? callback.onFulfilled : callback.onRejected
if (!cb) {
// 没定义 onFulfilled 或者 onRejected
cb = this.state === 'fullfilled' ? callback.resolve : callback.reject
cb(this.value)
return
}
const ret = cb()
cb(ret)
}
then(onFulfilled = null, onRejected = null){
return new Promise((resolve, reject) => {
this._handle(onFulfilled, onRejected, resolve, reject)
}
}
}
class Promise{
state = 'pending'
callbacks = []
value = undefined
constructor(fn){
fn(this._resolve.bind(this), this._reject.bind(this)
}
_resolve(value){
this.state = 'fullfiled'
this.value = value
this.callbacks.foreach(callback => this._handle(callback))
}
_reject(error){
this.state = 'rejected'
this.value = error
this.callbacks.foreach(callback => this._handle(callback))
}
_handle(callback){
if( this.state === 'pending') {
this.callbacks.push(callback)
return
}
let cb = this.state === 'fullfilled' ? callback.onFulfilled : callback.onRejected
if (!cb) {
// 没定义 onFulfilled 或者 onRejected
cb = this.state === 'fullfilled' ? callback.resolve : callback.reject
cb(this.value)
return
}
const ret = cb()
cb(ret)
}
then(onFulfilled = null, onRejected = null){
// 每次 then 都会创建新的 Promise 实例
return new Promise((resolve, reject) => {
this._handle(onFulfilled, onRejected, resolve, reject)
}
}
catch(onError){
// onFulfill为null,仅在onReject里面被调用
return this.then(null, onError)
}
}
class Promise {
static resolve(value) {
if(value && value instanceof Promise){
return value
} else if( value && typeof value === 'object' && typeof value.then === 'function') {
const then = value.then
return new Promise( resolve => then(resolve))
} else {
return new Promise(resolve => resolve(value))
}
}
}
class Promise {
static resolve(value) {
if(value && value instanceof Promise){
return value
} else if( value && typeof value === 'object' && typeof value.then === 'function') {
const then = value.then
return new Promise( resolve => then(resolve))
} else {
return new Promise(resolve => resolve())
}
}
static reject(value) {
if (value && typeof value === 'object' && typeof value.then === 'function') {
let then = value.then;
return new Promise((resolve, reject) => {
then(reject);
});
} else {
return new Promise((resolve, reject) => reject(value));
}
}
finally(onDone) {
const Promise = this.constructor;
return this.then(
value => Promise.resolve(onDone()).then(() => value),
reason => Promise.resolve(onDone()).then(() => { throw reason })
);
}
}