3 回答
TA贡献1811条经验 获得超4个赞
所有进入myFetch里的请求都会被缓存进一个数组
function AjaxFuc() {
let flag = true;
const bufferArray = [];
return function myFetch(i) {
if (flag) {
flag = false;
setTimeout(() => {
console.log(i);
flag = true;
if ((i = bufferArray.shift() )!== undefined) {
myFetch(i);
}
}, i % 2 === 0 ? 200 : 300);
} else {
bufferArray.push(i);
}
}
}
var myFetch = AjaxFuc();
let a = [1, 2, 3, 4, 5];
a.map((v, i) => {
this.myFetch(v);
})
TA贡献1852条经验 获得超7个赞
const o={
ajaxFun:function(i){
return new Promise((resolve,reject)=>{
setTimeout(function(){
console.log(i);
resolve(i);
},i%2==0?200:300)
});
},
mounted:function(){
const a=[1,2,3,4,5,6];
var g=a[Symbol.iterator]();
var me=this;
function next(){
var v=g.next().value;
if(v){
me.ajaxFun(v).then(next);
}
}
next();
}
}
o.mounted();
添加回答
举报