diff --git a/index.html b/index.html
index 1441e42..2dde5c0 100644
--- a/index.html
+++ b/index.html
@@ -26,7 +26,7 @@
res(22)
},1000)
})
- console.log(pp)
+ // console.log(pp)
pp.then(res => {
console.log(res)
}, err => {console.log(err.message)})
@@ -34,7 +34,7 @@
return new Promise(resolve => {
setTimeout(() => {
resolve(res)
- })
+ }, 1000)
})
}, err => console.log(err.message))
diff --git "a/\345\256\236\347\216\260\344\270\200\344\270\252\347\256\200\345\215\225\347\232\204promise.js" "b/\345\256\236\347\216\260\344\270\200\344\270\252\347\256\200\345\215\225\347\232\204promise.js"
index 2ecfd16..9053eb3 100644
--- "a/\345\256\236\347\216\260\344\270\200\344\270\252\347\256\200\345\215\225\347\232\204promise.js"
+++ "b/\345\256\236\347\216\260\344\270\200\344\270\252\347\256\200\345\215\225\347\232\204promise.js"
@@ -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)