
一句话思路调两次先将原生fetch保存下来修改原生fetch在原生fetch中去用保存下来的原生fetch去做真实的请求期间不用等待直接throw真实请求的promise出去真实请求成功或失败都将数据保存在缓存变量中然后在throw出去的真实请求promise中等待完成后再执行一遍业务函数。functionfn1(){varvfn2();console.log(1,v)}functionfn2(){returnfetch(https://api.thedogapi.com/v1/images/search?include_breed1breed_id%27)}run(fn1)functionrun(func){varoldfetchwindow.fetch;varcache{status:pending,value:null,}// 改造fetchfunctionnewFetch(...args){if(cache.statusfullfiled){returncache.value;}elseif(cache.statusreject){throwcache.value}varpoldfetch(...args).then(resres.json()).then((value){cache.statusfullfiledcache.valuevalue}).catch((err){cache.statusrejectcache.valueerr})throwp;}window.fetchnewFetch// 执行functry{func()}catch(e){// 如果是promiseif(einstanceofPromise){e.finally((){window.fetchnewFetch;func();window.fetcholdfetch;})}}// 恢复fetchwindow.fetcholdfetch}