0
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

ThinkGeek
  • 4,749
  • 13
  • 44
  • 91
  • 3
    does it help returning the fetch result? `return fetch().then()` – mbehzad Sep 11 '17 at 14:26
  • 1
    the `calls.map` arrow function callbacck has a body but that is missing the `return` statement/keyword. therefor the calls.map is resolved to `[undefinde, undefined,...]` – mbehzad Sep 11 '17 at 14:49
  • Cool after adding return statement, it is working but now in statement makeBatchCalls([1,2,3,4,5,6,7,8,9,10,12,12,13,14,15,16,17,18,19,20],3).then((responses) => console.log(responses)), this console.log() prints [undefined, undefined ....] – ThinkGeek Sep 11 '17 at 14:52
  • 1
    does `makeservercall` return anything? – mbehzad Sep 11 '17 at 14:54
  • .then((response) => makeservercall(response)) and makeservercall doesn't return anything. It is doing another fetch. – ThinkGeek Sep 11 '17 at 14:56
  • 1
    well, `await Promise.all(..)` returns what in the promise chain at the end is returned. and that is from `makeservercall ` which is nothing, therefor `undefined`. What would you like for the makeBatchCalls() to return? fetched values? or only true / OK? – mbehzad Sep 11 '17 at 15:00
  • I want all the responses from fetch(`https://jsonplaceholder.typicode.com/posts/${call}`) to be returned. – ThinkGeek Sep 11 '17 at 15:01
  • 1
    you could do: `fetch(`https://jsonplaceholder.typicode.com/posts/${call}`) .then((fetchResult) => {makeservercall().then(() => fetchResult}) })` – mbehzad Sep 11 '17 at 15:03
  • now responses contains two dimensional array [[response, respoonse, response], [response, response, response] ....] – ThinkGeek Sep 11 '17 at 15:08
  • 1
    Isn't that what you wanted when splitting them in two arrays? You can unpack them with console.log(...results) – mbehzad Sep 11 '17 at 15:20
  • ya actually, I unpacked them responses = [].split(...responses). – ThinkGeek Sep 11 '17 at 15:21
  • All done but fetch inside makeservercall is not making a server call. Do you see any issue why fetch inside makeservercall wont work? – ThinkGeek Sep 11 '17 at 15:22
  • 1
    could you also add the code for the `makeservercall ` method? – mbehzad Sep 11 '17 at 17:47
  • it worked, thanks for all the help here (y) – ThinkGeek Sep 12 '17 at 03:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154258/discussion-between-lokesh-agrawal-and-user2520818). – ThinkGeek Sep 12 '17 at 17:22

0 Answers0