场景:接口请求太快,想造成请求慢的假象
思路:
- 同时满足两个条件,即接口已经返回且时间不小于3s
代码:
function getTwoJson(params){
return fetch('./2.json').then(res=>{
return res.json()
})
}
function fetchData(apiFun){
return function(args){
return apiFun(args)
}
}
let getThree = fetchData(getTwoJson)
let delay = function(timeout){
return new Promise((resolve,reject)=>{
setTimeout(()=>{
resolve()
},timeout)
})
}
Promise.all([getThree({pageNumber:2,pageSize:10}),delay(3000)]).then(([res])=>{
console.log(res);
}).catch(err=>{
console.log(err);
})
Promise.all([fetchFun(api),delay(3000)]).then(([res])=>{
console.log(res);
})