async await to batch calls to server

877 Views Asked by At
async function makeBatchCalls(arrayIds, length) {
    let test = arrayIds.reduce((rows, key, index) => (index % length == 0 
                ? rows.push([key]) 
                : rows[rows.length-1].push(key)) && rows, []);
    let Batchresults = []; //convert them to two dimensionl arrays of given length [[1,2,3,4,5], [6,7,8,9,10]]
    for (calls of test) {
            Batchresults.push(await Promise.all(calls.map((call)=>{
                fetch(`https://jsonplaceholder.typicode.com/posts/${call}`)
                .then(() => makeservercall())
                })
            ));
    }
return Promise.all(Batchresults); //wait for all batch calls to finish
}

makeBatchCalls([1,2,3,4,5,6,7,8,9,10,12,12,13,14,15,16,17,18,19,20],3)

Code above works fine if I don't add the then call which makes a server call. Any pointer here is appreciated.

Linked request: JavaScript, React - sending multiple simultaneous ajax calls

0

There are 0 best solutions below