promiseA+中的 then方法
- promise必须返回一个then方法
promise2 = promise1. then(onFulfilled, onRejected);
- 执行成功/失败的回调函数,返回值为x
If either onFulfilled or onRejected returns a value x
- 如果一旦回调函数中报错,抛出异常,则被catch捕获
# 1. 链式调用返回普通值情况处理
let fs = require('fs')
let Promise = require('. /promise)
let p1 = new Promise((resolve, reject) => {
resolve(100)
})
let p2 = p1. then((data) => {
return 1
})
p2. then((data) => {
console. log(data, 'p2----data')
})
# 链式调用返回普通值得部分源码
then(onFulfilled, onRejected) {
let promise2 = new Promise((resolve, reject) => {
if(this.status == RESOLVED) {
let x = onFulfilled(this.value); // 1 普通值
resolve(x); // 执行p2.resolve
}
if(this.status == REJECTED) {
onRejected(this.reason);
resolve(x); // 执行p2.resolve
}
if(this.status == PENDING) {
this.onResolvedCallbacks.push(_ => {
// todo...
let x = onFulfilled(this.value);
resolve(x);
})
this.onRejectedCallbacks.push(_ => {
// todo...
let x = onRejected(this.reason);
resolve(x);
})
}
})
}
# 2. 链式调用返回promise情况处理
let fs = require('fs')
let Promise = require('. /promise)
// p2的返回值是 promise
let p1 = new Promise((resolve, reject) => {
resolve(100)
})
let p2 = p1. then((data) => {
return new Promise((resolve, reject) => {
setTimeout(_ => {
resolve('ok')
}, 1000)
})
}, err => {
return '失败';
})
p2. then((data) => {
console. log(data, 'p2----data')
})
// p2的回调中 抛了异常
let p1 = new Promise((resolve, reject) => {
resolve(100)
})
let p2 = p1. then((data) => {
throw new Error();
}, err => {
return '失败';
})
p2. then((data) => {
console. log(data, 'p2----data')
})
const PENDING = 'PENDING';
const RESOLVED = 'RESOLVED';
const REJECTED = 'REJECTED';
const resolvePromise = (promise2, x, resolve, reject) => {
}
class Promise{
constructor(executor){
this.status = PENDING;
this.value = undefined;
this.reason = undefined;
this.onResolvedCallbacks = []; // 存储成功时需要执行的回调函数
this.onRejectedCallbacks = []; // 存储失败时需要执行的回调函数
let resolve = (value) => {
if(this.status === PENDING) {
this.status = RESOLVED;
this.value = value;
// 将 pending状态时,存储的成功的回调 依次执行
this.onResolvedCallbacks.forEach( fn => fn());
}
}
let reject = (reason) => {
if(this.status === PENDING) {
this.status = REJECTED;
this.reason = reason;
this.onRejectedCallbacks.forEach( fn => fn());
}
}
try{
executor(resolve, reject); // 立即执行
} catch(e) {
reject(e)
}
}
then(onFulfilled, onRejected) {
let promise2 = new Promise((resolve, reject) => {
if(this.status == RESOLVED) {
setTimeout(_ => {
try {
let x = onFulfilled(this.value);
// x 可能是一个promise
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e)
}
}, 0)
}
if(this.status == REJECTED) {
setTimeout(_ => {
try {
let x = onRejected(this.reason);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e)
}
}, 0)
}
if(this.status == PENDING) {
this.onResolvedCallbacks.push(_ => {
setTimeout(_ => {
try {
// todo...
let x = onFulfilled(this.value);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e)
}
}, 0)
})
this.onRejectedCallbacks.push(_ => {
setTimeout(_ => {
try {
// todo...
let x = onRejected(this.reason);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e)
}
}, 0)
})
}
})
}
}
# promise2和x引用的同一个变量
const resolvePromise = (promise2, x, resolve, reject) => {
// 1、循环引用 自己等待自己完成
if(promise2 === x) { // 用一个类型错误 结束掉promise
return reject(new TypeError('Chainig cycle detected for promise...'))
}
}