Skip to content

Commit

Permalink
[add] a sinple promise library
Browse files Browse the repository at this point in the history
  • Loading branch information
Copyes committed Aug 20, 2017
1 parent 8d49e1a commit 1004ec5
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
115 changes: 115 additions & 0 deletions Promise/Promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* the constructor of CPromise
* @param {*} executor a callback
*/
function CPromise(executor){
var self = this
this.status = 'pending'
this.data = undefined
// when promise resolved, the collections contains more than one callback,
// and two callback at most
this.onResolvedCallback = []
this.onRejectedCallback = []

// the status is fulfilled
function resolve(value){
if(self.status === 'pending'){
self.status = 'resolved'
self.data = value
for(var i = 0; i < self.onResolvedCallback.length; i++){
self.onResolvedCallback[i](value)
}
}
}
// the status is rejection
function reject(reason){
if(self.status === 'pending'){
self.status = 'rejected'
self.data = reason
for(var i = 0; i < self.onRejectedCallback.length; i++){
self.onRejectedCallback[i](reason)
}
}
}
// executor may be wrong, so we must catch the mistake
try {
executor(resolve, reject)
}catch(e){
reject(e)
}
}
// a method named then that mounted on the CPromise.prototype
CPromise.prototype.then = function(onResolved, onRejected){
var self = this
var otherPromise
onResolved = typeof onResolved === 'function' ? onResolved : function(v) { return v }
onRejected = typeof onRejected === 'function' ? onRejected : function(r) { return e }
if(self.status === 'resolved'){
return otherPromise = new CPromise(function(resolve, reject){
try{
var temp = onResolved(self.data)
temp instanceof CPromise && temp.then(resolve, reject)
resolve(temp)
}catch(err){
reject(err)
}
})
}

if(self.status === 'rejected'){
return otherPromise = new CPromise(function(resolve, reject){
try{
var temp = onRejected(self.data)
temp instanceof CPromise && temp.then(resolve, reject)
}catch(err){
reject(err)
}
})
}
// if the status is always pending,
// the callback is pushed to the corresponding array
if(self.status === 'pending'){
return otherPromise = new CPromise(function(resolve, reject){
// resolved callback array, return a promise
self.onResolvedCallback.push(function(value){
try{
var temp = onResolved(self.data)
temp instanceof CPromise && temp.then(resolve, reject)
resolve(temp)
}catch(err){
reject(err)
}
})
// rejected callback array
self.onRejectedCallback.push(function(){
try{
var temp = onRejected(self.data)
temp instanceof CPromise && temp.then(resolve, reject)
}catch(err){
reject(err)
}
})
})
}
}
// a method named catch that mounted on the CPromise.prototype
CPromise.prototype.catch = function(onRejected){
return this.then(null, onRejected)
}
// when a big error happened, run stop ,
// the function will stop the next action
CPromise.prototype.stop = function(){
return new CPromise(function(){})
}
CPromise.prototype.done = function(){
return this.catch(function(err){
console.log(err)
})
}

module.exports = {
CPromise
}



13 changes: 13 additions & 0 deletions Promise/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var { CPromise } = require('./Promise.js')
// usage:
new CPromise(function(resolve, reject) { // 我们实现的Promise
// setTimeout(function() {
// resolve(42)
// }, 2000)
resolve(42)
}).then()
.then()
.then(function(val){
console.log(val)
})
.done()

0 comments on commit 1004ec5

Please sign in to comment.