码农干货系列【13】--Promise库Version4 - 【当耐特】
by 【当耐特】
at 2013-04-24 12:24:00
original http://www.cnblogs.com/iamzhanglei/archive/2013/04/24/3039941.html
代码
var Promise = function () { this.thens = []; }; Promise.prototype = { resolve: function () { if (this.promiseArr) { if (this.promiseArr[0].action !== "any") { for (var i = 0, j = this.promiseArr.length; i < j; i++) { this.promiseArr[i].resolveCount++; } if (this.resolveCount !== this.promiseArr.length) { return; } } } var t, n; while (t = this.thens.shift()) { var doneFn = t.done, action = t.action; if (doneFn.length) { var arr = []; for (var i = 0, j = doneFn.length; i < j; i++) { var m = doneFn[i].apply(null, arguments); if (m instanceof Promise) { m.thens = this.thens; arr.push(m); } } var l = arr.length; if (l === 0) { continue; } else { for (var i = 0; i < l; i++) { arr[i].promiseArr = arr; arr[i].action = action; arr[i].resolveCount = 0; } break; } } else { n = doneFn.apply(null, arguments); if (n instanceof Promise) { n.thens = this.thens; break; } continue; } } }, reject: function () { if (this.promiseArr && this.promiseArr[0].action === "any") { if (this.promiseArr[this.promiseArr.length - 1] !== this) { return; } } var t; while (t = this.thens.shift()) { if (t.fail) t.fail.apply(null, arguments); break; } }, then: function (done, fail) { this.thens.push({ done: done, fail: fail, action: "then" }); return this; }, any: function (done, fail) { this.thens.push({ done: done, fail: fail, action: "any" }); return this; } }
更新
增加any方法,任何一个task完成都出发resolve
去除_handle方法
测试
f1() .then(function () { console.log(arguments) return f2(); }) .then(function () { f3(); }) .then(f4) .then(f5) .then([f6, f7]) .any([f8, f9, f10]) .then(f11, function () { console.log("fail"); }) function f1() { var promise = new Promise(); setTimeout(function () { console.log(1); promise.resolve("from f1"); }, 1500) return promise; } function f2() { var promise = new Promise(); setTimeout(function () { console.log(2); promise.resolve(); }, 1500) return promise; } function f3() { var promise = new Promise(); setTimeout(function () { console.log(3); promise.resolve(); }, 1500) return promise; } function f4() { var promise = new Promise(); setTimeout(function () { console.log(4); promise.resolve(); }, 1500) return promise; } function f5() { console.log(5); } function f6() { var promise = new Promise(); setTimeout(function () { console.log(6); promise.resolve(); }, 1500) return promise; } function f7() { var promise = new Promise(); setTimeout(function () { console.log(7); promise.resolve(); }, 5500) return promise; } function f8() { var promise = new Promise(); setTimeout(function () { console.log(8); promise.reject(); }, 1500) return promise; } function f9() { var promise = new Promise(); setTimeout(function () { console.log(9); promise.reject(); }, 1500) return promise; } function f10() { var promise = new Promise(); setTimeout(function () { console.log(10); promise.reject(); }, 4500) return promise; } function f11() { var promise = new Promise(); setTimeout(function () { console.log(11); promise.resolve(); }, 1500) return promise; }
本文链接:http://www.cnblogs.com/iamzhanglei/archive/2013/04/24/3039941.html,转载请注明。