Skip to content

Commit

Permalink
write promise
Browse files Browse the repository at this point in the history
  • Loading branch information
abstain23 committed Jun 5, 2020
1 parent 3def4a1 commit ef5fa72
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 43 deletions.
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
res(22)
},1000)
})
console.log(pp)
// console.log(pp)
pp.then(res => {
console.log(res)
}, err => {console.log(err.message)})
const a = pp.then(res => {
return new Promise(resolve => {
setTimeout(() => {
resolve(res)
})
}, 1000)
})
}, err => console.log(err.message))

Expand Down
99 changes: 58 additions & 41 deletions 实现一个简单的promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,91 +43,108 @@
/**
* excutor 执行器函数(同步)
*/
!function(){
!function () {
function Promise(excutor) {
this.status = 'pendding'
this.data = undefined
this.callbacks = [] //{onResloved:() => {}, onRejected:() => {}}
// let _this = this
const resolve = (value) => {
if(this.status !== 'pendding') return
const resolve = (value) => {
if (this.status !== 'pendding') return
this.status = 'resolved'
this.data = value
if(this.callbacks.length > 0) {
if (this.callbacks.length > 0) {
setTimeout(() => {
this.callbacks.forEach(cbObj => {
cbObj['onResolved'](value)
})
},0)
}, 0)
}
}

const reject = (reason) => {
if(this.status !== 'pendding') return
if (this.status !== 'pendding') return
this.status = 'rejected'
this.data = reason
if(this.callbacks.length > 0) {
if (this.callbacks.length > 0) {
setTimeout(() => {
this.callbacks.forEach(cbObj => {
cbObj['onRejected'](value)
})
},0)
}, 0)
}
}
try {
excutor(resolve, reject)
}catch(err) {
} catch (err) {
reject(err)
}
}

Promise.prototype.then = function (onResolved, onRejected) {

onResolved = typeof onResolved === 'function' ? onResolved : value => value
onRejected = typeof onRejected === 'function' ? onRejected : reason => {throw reason}

Promise.prototype.then = function(onResolved, onRejected) {
return new Promise((resolve, reject) => {
const handle = (callback) => {
try {
const res = callback(this.data)
if (res instanceof Promise) {
// res.then(value => resolve(value), err => reject(err))
res.then(resolve, reject)
} else {
resolve(res)
}
} catch (error) {
reject(error)
}
}
// console.log(this.status)
if(this.status === 'pendding') {
this.callbacks.push({onResolved, onRejected})
} else if(this.status === 'resolved') {
setTimeout(() => {
try {
const res = onResolved(this.data)
if(res instanceof Promise) {
// res.then(value => resolve(value), err => reject(err))
res.then(resolve, reject)
} else {
resolve(res)
}
} catch (error) {
reject(error)
if (this.status === 'pendding') {
this.callbacks.push({
onResolved: (value) => {
// onResolved(this.data)
// onResolved(value)
handle(onResolved)
},
onRejected: (reason) => {
// onRejected(reason)
handle(onRejected)
}
})
} else {
} else if (this.status === 'resolved') {
setTimeout(() => {
onRejected(this.data)
handle(onResolved)
// resolve(this.data)
})
} else {
setTimeout(() => {
handle(onRejected)
});
}
})
}

Promise.prototype.catch = function (onRejected) {
return this.then(undefined, onRejected)
}

Promise.prototype.catch = function(onRejected) {

}

Promise.resolve = function(value) {

}
Promise.resolve = function (value) {

}

Promise.rejected = function(reason) {
Promise.rejected = function (reason) {

}
}

Promise.all = function(promises) {
Promise.all = function (promises) {

}
}

Promise.race = function(promises) {
Promise.race = function (promises) {

}
window.Promise = Promise
}(window)
}
window.Promise = Promise
}(window)

0 comments on commit ef5fa72

Please sign in to comment.