呈现问题描写:当不确定异步请求个数时,为防备当一瞬间产生上百个http请求时,导致会萃了无数挪用栈进而导致内存溢出问题。
要求:将同一时刻并发请求数量节制在3个以内,同时还要尽大概快速的拿到响应的功效。
同口试问题:
实现一个批量请求函数 multiRequest(urls, maxNum),要求如下:
要求最大并发数 maxNum
每当有一个请求返回,就留下一个空位,可以增加新的请求
所有请求完成后,功效凭据 urls 内里的顺序依次打出
1、基于Promise.all实现Ajax的串行和并行平时都是基于promise来封装异步请求的
串行:一个异步请求完成了之后再举办下一个请求
并行:多个异步请求同时举办
示例:串行
var p = function () { return new Promise(function (resolve, reject) { setTimeout(() => { console.log('1000') resolve() }, 1000) }) } var p1 = function () { return new Promise(function (resolve, reject) { setTimeout(() => { console.log('2000') resolve() }, 2000) }) } var p2 = function () { return new Promise(function (resolve, reject) { setTimeout(() => { console.log('3000') resolve() }, 3000) }) } p().then(() => { return p1() }).then(() => { return p2() }).then(() => { console.log('end') })
并行:
var promises = function () { return [1000, 2000, 3000].map(current => { return new Promise(function (resolve, reject) { setTimeout(() => { console.log(current) }, current) }) }) } Promise.all(promises()).then(() => { console.log('end') }) Promise.all(promises: []).then(fun: function);
promise.all担保数组中所有promise工具都到达resolve状态,才执行then回调
Promise.all并发限制寄义: 指每个时刻并发执行的promise数量是牢靠的,最终执行的功效照旧保持与本来的promise.all一致。
思路与实现
回收递归挪用来实现,配置最大请求数量上限。并在这些请求中的每一个都应该在完成时继承递归发送,通过传入的索引来确定了urls内里详细是谁人URL,担保最后输出的顺序不会乱,而是依次输出
代码实现:
function multiRequest(urls = [], maxNum) { // 请求总数量 const len = urls.length; // 按照请求数量建设一个数组来生存请求的功效 const result = new Array(len).fill(false); // 当前完成的数量 let count = 0; return new Promise((resolve, reject) => { // 请求maxNum个 while (count < maxNum) { next(); } function next() { let current = count++; // 处理惩罚界线条件 if (current >= len) { // 请求全部完成绩将promise置为乐成状态, 然后将result作为promise值返回 !result.includes(false) && resolve(result); return; } const url = urls[current]; console.log(`开始 ${current}`, new Date().toLocaleString()); fetch(url) .then((res) => { // 生存请求功效 result[current] = res; console.log(`完成 ${current}`, new Date().toLocaleString()); // 请求没有全部完成, 就递归 if (current < len) { next(); } }) .catch((err) => { console.log(`竣事 ${current}`, new Date().toLocaleString()); result[current] = err; // 请求没有全部完成, 就递归 if (current < len) { next(); } }); } }); }
代码实现:
// 任务列表->新建任务 uploadFile() { let _this = this; var uploadThreadLimitNums = 3, uploadThreadNums = 0, sendFinishNum = 0, resultFinishNum = 0; var marks = 0; var tasks = []; var upload = function () { while (uploadThreadNums < uploadThreadLimitNums) { if (sendFinishNum >= _this.fileList.length) { if (resultFinishNum >= _this.fileList.length) { creatTask(); // 完成请求 } return; } (function (j) { let item = _this.fileList[j]; let p = new FormData(); p.append("file", item); tasks.push( axios({ method: "post", url: `${window.UL_CONFIG.BASEURL}/api/files/upload`, data: p, onUploadProgress: (progressEvent) => { for (let i in _this.rowData) { _this.rowData[i].name === item.name ? (_this.rowData[i].percent = Math.round( (progressEvent.loaded / progressEvent.total) * 100 )) : ""; } }, }) .then((res) => { /* let obj = {}; obj.url = `${window.UL_CONFIG.BASEURL}/api/files/${res.data}`; obj.fileName = item.name; obj.fmt = _this.ruleForm.format; obj.samplingRate = _this.ruleForm.samplingRate; fileUrls.push(obj); */ }) .catch((e) => { ? (_this.rowData[i].percent = 0) _this.$notify.error({ title: "错误", message: "处事毗连错误 " + item.name + " 未上传乐成", }); .finally(() => { uploadThreadNums--; resultFinishNum++; upload(); ); })(sendFinishNum); uploadThreadNums++; sendFinishNum++; } }; var creatTask = function () { axios.all(tasks).then((res) => { // 新建上传任务 /* let fd1, fd2, calcFlag, flagArr, language; fd1 = {}; flagArr = Object.assign([], _this.ruleForm.checkList); if (_this.ruleForm.recognize == "自动识别") { flagArr.push("2"); calcFlag = flagArr.reduce( (accu, curr) => Number(accu) + Number(curr) ); _this.ruleForm.recognize == "自动识别" ? (language = "") : (language = _this.ruleForm.recognize); fd1.processContent = calcFlag; fd1.remark = _this.ruleForm.remark; fd1.name = _this.ruleForm.taskName; fd1.fmt = _this.ruleForm.format; fd1.samplingRate = _this.ruleForm.samplingRate; fd1.language = language; fd1.type = 1; // type: 1 语音, 2 视频 fd1.files = fileUrls; */ newTask(fd1).then((res) => { /* _this.cmpltBtnState = false; _this.$store.commit("setTaskId", res.data.id); _this.submitFailNumber = res.data.submitFailNumber; */ _this.$parent.dataInit(); }); }); upload(); },