模拟Promise
; (function() { var Ajax = function(config) { var me = this; switch (config.type) { case "post": me.post(config); break; case "get": me.get(config); break; } } Ajax.prototype = { constructor: Ajax, get: function(config) { var xml = new XMLHttpRequest(); xml.onload = function() { if (xml.status === 200) { config.callback("1212"); } } xml.open("get", config.url); xml.send(null); }, post: function(config) { var me = this; var xml = new XMLHttpRequest(); var param = new FormData(); Object.keys(config.params).forEach(i => { param.append(i, config.params[i]); }); xml.onload = function() { if (xml.status === 200) { config.callback("1212"); } } xml.open("post", config.url); xml.send(param); } }; var PROMISE = function(callback) { var me = this; me.result = []; me.total = 0; Object.defineProperty(me, "flag", { set: function(v) { var me = this; if (v) { me.funcStack(me.r); } } }); var resolve = function(r) { me.r = r; me.flag = true; } var reject = function(r) { me.flag = false; } setTimeout(callback.bind(me, resolve.bind(me), reject.bind(me)), 0); }; PROMISE.prototype.then = function(callback) { var me = this; me.funcStack = callback; return me; } PROMISE.all = function(arr) { console.log(arr instanceof Array) if (!(arr instanceof Array)) { throw (new Error("输入错误")); return; } var me = new PROMISE(function(r1, r2) { var me = this; count = 0; Object.defineProperty(me, "total", { set: function(v) { console.log(count); if (count === arr.length) { r1(me.result); } } }); arr.forEach(i => { i.then.call(i, function(r) { me.result.push(r); count += 1; me.total = 1; }) }) }); return me; } var t=Date.now(); var tem = function() { var arr = []; for (let i = 1; i <=6; i += 1) { arr.push(new PROMISE(function(r1, r2) { var me = this; var img = new Image() img.onload = () => { r1("1221") }; img.src = "imgs/"+i+".jpg"; document.querySelector(".wrap").append(img); }).then(function(r, callback) {})); } return arr; } PROMISE.all([...tem()]).then(function(r) { console.log(Date.now()-t); console.log(r); }) })()
在定义函数then拦截函数 然后使用触发器 触发拦截的函数 在Promise.all拦截的函数加载了 一个函数可以统计最后的结果