(function (w) { /* ************************* Promise Function ************************* */ function MyPromise(processor) { var self = this; self.status = 'pending'; // status self.value = null; // resolve value self.reason = null; // reject reason self.onRejectedCallbacks = []; // reject callback self.onFulfilledCallbacks = []; // resolve callback // resolve // function resolve(value) { if (self.status !== 'pending') return; var resolveDo = function (_value) { self.status = 'fulfilled'; self.value = _value; self.onFulfilledCallbacks.mforEach(function (callback) { callback(); }); }; if (value instanceof MyPromise) { value.then(function (_value) { resolveDo(_value); }, function (_value) { reject(_value); }); } else { resolveDo(value); } } // reject // function reject(reason) { if (self.status !== 'pending') return; self.status = 'rejected'; self.reason = reason; self.onRejectedCallbacks.mforEach(function (callback) { callback(reason); }); } // delay to next event loop // try { processor(resolve, reject); } catch (e) { setTimeout(function () { reject(e); }); } } /* ------------------- 分析promise.then中返回的值类型 promise/value ------------------- */ /** * [analysisPromise 使用递归将状态控制权转移] * @param {[Any]} x [value] * @param {[Func]} resolve [get into success state] * @param {[Func]} reject [get into faill state] */ var analysisPromise = function (x, resolve, reject) { var then, y; if (x !== undefined && (typeof x === 'object' || typeof x === 'function')) { then = x.then; // obj Promise if (then && typeof then === 'function') { then.call(x, function (value) { // callback return a promise analysisPromise(value, resolve, reject); }, function (error) { reject(error); }); // normal }else { resolve(x); } // normal }else { resolve(x); } }; /* ------------------- then-返回新的promise ------------------- */ /** * [then 应该返回一个全新的Promise对象,不应该与当前Promise存在功能耦合] * @param {[type]} successFn [description] * @param {[type]} errorFn [description] */ MyPromise.prototype.then = function (successCallback, errorCallback) { var promise, x; var self = this; if (self.status === 'fulfilled') { promise = new MyPromise(function (resolve, reject) { // delay to next event loop setTimeout(function () { try { x = successCallback ? successCallback(self.value) : self.value; analysisPromise(x, resolve, reject); } catch (e) { reject(e); } }); }); }else if (self.status === 'rejected') { promise = new MyPromise(function (resolve, reject) { // delay to next event loop setTimeout(function () { try { x = errorCallback ? errorCallback(self.reason) : MyPromise.reject(self.reason); analysisPromise(x, resolve, reject); } catch (e) { reject(e); } }); }); }else if (self.status === 'pending') { promise = new MyPromise(function (resolve, reject) { self.onFulfilledCallbacks.push(function () { try { x = successCallback ? successCallback(self.value) : self.value; // 分析返回值 然后更改 当前promise状态 analysisPromise(x, resolve, reject); } catch (e) { reject(e); } }); self.onRejectedCallbacks.push(function () { try { x = errorCallback ? errorCallback(self.reason) : MyPromise.reject(self.reason); // 分析返回值 然后更改 当前promise状态 analysisPromise(x, resolve, reject); } catch (e) { reject(e); } }); // 延迟到下一个事件循环 setTimeout(function () { }) }); } return promise; }; /* ------------------- 错误捕获 ------------------- */ MyPromise.prototype.mcatch = function (handleError) { var self = this; var promise = new MyPromise(function (resolve, reject) { try { analysisPromise(self, function(result) { // resolved -> 返回原始结果 resolve(result); }, function(error) { // rejected -> 处理错误 -> 返回resolved结果 handleError(error); resolve(error); }); } catch (e) { // catch error -> 返回resolved结果 resolve(e); } }); return promise; }; /* ------------------- all-所有promise成功后即成功,一个失败即失败 ------------------- */ MyPromise.all = function (pArray) { var rArray = []; return new MyPromise(function (resolve, reject) { var count = 0; pArray.mforEach(function (pr, i) { if (pr instanceof MyPromise) { pr.then(function (value1) { analysisPromise(value1, function (value2) { rArray[i] = value2; count++; if (count === pArray.length) { // console.log("rArray",rArray); resolve(rArray); } }, reject); }, function (error) { reject(error); }); }else { rArray[i] = pr; if (rArray.length === pArray.length) resolve(rArray); } }); }); }; /* ------------------- 返回成功态的promise ------------------- */ MyPromise.resolve = function (value) { return (value instanceof MyPromise) ? value : new MyPromise(function (resolve, reject) { resolve(value); }); }; /* ------------------- 返回失败态的promise ------------------- */ MyPromise.reject = function (reason) { return (reason instanceof MyPromise) ? value : new MyPromise(function (resolve, reject) { reject(reason); }); }; w.MPromise = MyPromise; })(window);