Promise并发请求,单个请求完毕后立刻处理请求

getJSON('xxxx')

.then(function(response) {

  return Promise.all(response.results.map(getJSON));

})

.then(function(series) {

  series.forEach(addDom);

});

其中getJSON() 是fetch请求,response.results是url的数组,addDom()是对数据的操作;

这段代码实现了:1.url并发请求 2.不同请求的数据顺序操作

但问题是所有请求全部加载完成后再处理数据,如何实现当前顺序的数据加载完成后就立刻对其处理而不等待其他数据


Helenr
浏览 1204回答 1
1回答

慕工程0101907

写了个例子,可以在chrome的控制台里直接执行(()=>{&nbsp; &nbsp; Promise.allInOrder = (promises, thenForEach)=>{&nbsp; &nbsp; &nbsp; &nbsp; let callBackQueue = new Array(promises.length);&nbsp; &nbsp; &nbsp; &nbsp; let thenCallbackQueue = new Array(promises.length);&nbsp; &nbsp; &nbsp; &nbsp; let queueExecutor = ()=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(let i=0,l=callBackQueue.length;i<l;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(callBackQueue[i] == void(0)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // undefined means this task is not done yet&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // execute callback&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callBackQueue[i]();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // set this callback to null since it has been executed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callBackQueue[i] = ()=>{};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // when reach here, it means all callbacks been executed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Promise.all(thenCallbackQueue).then(ds=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; allPromiseResolver(ds);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; let allPromiseResolver = null,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; allPromiseRejector = null;&nbsp; &nbsp; &nbsp; &nbsp; for(let i=0,l=promises.length;i<l;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; promises[i].then(d=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callBackQueue[i] = function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; thenCallbackQueue[i] = new Promise((resolve, reject)=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let result = thenForEach(d);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resolve(result);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; queueExecutor();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; all: callback=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new Promise((resolve, reject)=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; allPromiseResolver = resolve;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; allPromiseRejector = reject;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).then(ds=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callback(ds);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };})();// test(()=>{&nbsp; &nbsp; let timeConsumingFunc = param=>new Promise(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (resolve)=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let timeout = Math.random() * 5000;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(`task ${param} will be resolved in ${timeout}ms`);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTimeout(()=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(`${param} resolved`);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resolve(param+10);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, timeout);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; Promise&nbsp; &nbsp; &nbsp; &nbsp; .allInOrder(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [timeConsumingFunc(1), timeConsumingFunc(2), timeConsumingFunc(3)],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(d);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return d+20;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; .all((ds)=>{console.log(`all method called`);console.log(ds);})})();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript