var promise = (function()
if (!(this instanceof promise)) return new promise(resolver)
var self = this //儲存this
self.callbacks = //儲存onresolve和onreject函式集合
self.status = 'pending' //當前狀態
function resolve(value)
self.status = 'resolved' //修改狀態
self.data = value
for (var i = 0; i < self.callbacks.length; i++) })}
function reject(reason)
self.status = 'rejected' //修改狀態
self.data = reason
for (var i = 0; i < self.callbacks.length; i++) })}
try catch(e)
}function resolvepromise(promise, x, resolve, reject)
if ((x !== null) && ((typeof x === 'object') || (typeof x === 'function'))) , function rj(r) )
} else
} catch(e)
} else
}promise.prototype.then = function(onresolved, onrejected)
onrejected = typeof onrejected === 'function' ? onrejected : function(r)
var self = this
var promise2
//promise狀態為resolved
if (self.status === 'resolved') catch(e)
})})
}//promise狀態為rejected
if (self.status === 'rejected') catch(e)
})})
}//promise狀態為pending
//需要等待promise的狀態改變
if (self.status === 'pending') catch(e)
},onrejected: function(reason) catch(e) }})
})}
}//獲取當前promise傳遞的值
promise.prototype.valueof = function()
//由then方法實現catch方法
promise.prototype.catch = function(onrejected)
//finally方法
promise.prototype.finally = function(fn) , function(r))
}promise.prototype.spread = function(fn, onrejected) , onrejected)
}promise.prototype.inject = function(fn, onrejected) ))
}, onrejected)
}promise.prototype.delay = function(duration) , duration)
})}, function(reason) , duration)
})})
}promise.all = function(promises)
}, function(reason) )
})(i)}})
}promise.race = function(promises) , function(reason) )}})
}promise.resolve = function(value) )
return promise
}promise.reject = function(reason) )
}promise.fcall = function(fn)
promise.done = promise.stop = function())
}promise.deferred = promise.defer = function()
dfd.promise = new promise(function(resolve, reject) )
return dfd
}try catch(e) {}
return promise
})()
promise實現分析 簡單promise實現原理
promise可以有三種狀態,分別是pedding fulfilled rejected pending promise物件例項建立時候的初始狀態 fulfilled 可以理解為成功的狀態 rejected可以理解為失敗的狀態 構造乙個promise例項需要給promise建構函式傳入乙個函式。傳入...
簡單實現Promise原理
const pending pending const resolved resolved const rejected rejected 對於不太經常更改的變數 定於為常量 function mypromise fn function reject value trycatch e mypromi...
Promise原理 簡單實現
參考 個人認為原博的實現有點問題 在next函式的實現上,會導致無限的呼叫 看看一般promise的用法 promise new promise function resolve,reject then function val functioin err then function val fun...