非链式调用promise
const PENDING = ‘pending’
const FULFILLED = ‘fulfilled’
const REJECTRD = ‘rejected’
function Promise1 (executor) {
let _this = this
this.state = PENDING
this.value = undefined // 成功原因
this.reason = undefined // 失败原因
this.onFulfilled = [] // 成功的回调
this.onRejected = [] // 失败的回调
function resolve (value) {
console.log(‘resolve’)
if (_this.state === PENDING) {
_this.state = FULFILLED
_this.value = value
console.log(_this.onFulfilled)
_this.onFulfilled.forEach(fn => fn(value))
}
}
function reject (reason) {
console.log(‘reject’)
if (_this.state === PENDING) {
_this.state = REJECTRD
_this.reason = reason
_this.onRejected.forEach(fn => fn(reason))
}
}
try {
executor(resolve, reject)
} catch (e) {
reject(e)
}
}
Promise1.prototype.then = function (onFulfilled, onRejected) {
console.log(‘then-onFulfilled’, onFulfilled)
console.log(‘then-onRejected’, onRejected)
if (this.state === FULFILLED && typeof onFulfilled === ‘function’) {
onFulfilled(this.value)
}
if (this.state === REJECTRD && typeof onFulfilled === ‘function’) {
onRejected(this.value)
}
if (this.state === PENDING) {
typeof onFulfilled === ‘function’ && this.onFulfilled.push(onFulfilled)
typeof onRejected === ‘function’ && this.onRejected.push(onRejected)
}
}
var p = new Promise1((resolve, reject)=>{
setTimeout(() => {
resolve(4)
}, 1000)
})
p.then((res)=>{
console.log(res, ‘res’)
})
p.then((res1)=>{
console.log(res1, ‘res1’)
})
链式调用
测试
npm install -g promises-aplus-tests
promises-aplus-tests promise.js(文件名)
promises-aplus-tests 中共有 872 条测试用例。以上代码,可以完美通过所有用例。
const PENDING = ‘pending’
const FULFILLED = ‘fulfilled’
const REJECTRD = ‘rejected’
function Promise (executor) {
let _this = this
this.state = PENDING
this.value = undefined // 成功原因
this.reason = undefined // 失败原因
this.onFulfilled = [] // 成功的回调
this.onRejected = [] // 失败的回调
function resolve (value) {
if (_this.state === PENDING) {
_this.state = FULFILLED
_this.value = value
_this.onFulfilled.forEach(fn => fn(value))
}
}
function reject (reason) {
if (_this.state === PENDING) {
_this.state = REJECTRD
_this.reason = reason
_this.onRejected.forEach(fn => fn(reason))
}
}
try {
executor(resolve, reject)
} catch (e) {
reject(e)
}
}
Promise.prototype.then = function (onFulfilled, onRejected) {
let _this = this
onFulfilled = typeof onFulfilled === ‘function’ ? onFulfilled : value => value
onRejected = typeof onRejected === ‘function’ ? onRejected : reson => { throw reson }
let promise2 = new Promise((resolve, reject) => {
if (_this.state === FULFILLED) {
setTimeout(() => {
try {
let x = onFulfilled(_this.value)
resolvePromise1(promise2, x, resolve, reject)
} catch (error) {
reject(error)
}
})
} else if (_this.state === REJECTRD) {
setTimeout(() => {
try {
let x = onRejected(_this.reason)
resolvePromise1(promise2, x, resolve, reject)
} catch (error) {
reject(error)
}
})
} else if (_this.state === PENDING) {
_this.onFulfilled.push(() => {
setTimeout(() => {
try {
let x = onFulfilled(_this.value)
resolvePromise1(promise2, x, resolve, reject)
} catch (error) {
reject(error)
}
})
})
_this.onRejected.push(() => {
setTimeout(() => {
try {
let x = onRejected(_this.reason)
resolvePromise1(promise2, x, resolve, reject)
} catch (error) {
reject(error)
}
})
})
}
})
return promise2
}
function resolvePromise1 (promise2, x, resolve, reject) {
if (promise2 === x) {
reject(new TypeError(‘chanining cycle’))
}
if (x !== null && (typeof x === ‘object’ || typeof x === ‘function’)) {
// 函数和对象
let used;
try {
let then = x.then
if (typeof then === ‘function’) {
then.call(x, (y) => {
if (used) return
used = true
resolvePromise1(promise2, y, resolve, reject)
}, ® => {
if (used) return
used = true
reject®
})
} else {
if (used) return
used = true
resolve(x)
}
} catch (error) {
if (used) return
used = true
reject(error)
}
} else {
// 普通值
resolve(x)
}
}
// 测试使用
Promise.defer = Promise.deferred = function () {
let dfd = {};
dfd.promise = new Promise((resolve,reject)=>{
dfd.resolve = resolve;
dfd.reject = reject;
})
return dfd;
}
module.exports = Promise;