0

I am invoking multiple api calls using Promise.all and am able to receive their responses, but I want to add them to an array

my code

async function asynchUploadGrpFiles(files) {
  var responseDetails = [];

  await Promise.all([get('https://httpbin.org/get?a=1'),
    get('https://httpbin.org/get?b=2'),
    get('https://httpbin.org/get?c=3')
  ]).then(values => {

    responseDetails.push(":::: time :::" + values);

    console.log(values);
  });

  console.log(responseDetails.toString());
}

I am able to log the response from console.log(values);, but these are not getting pushed in responseDetails

Andreas
  • 21,535
  • 7
  • 47
  • 56
Saurabh Jhunjhunwala
  • 2,832
  • 3
  • 29
  • 57
  • Why do you use `async`/`await`? – Andreas Jan 18 '21 at 16:34
  • @Andreas I want this function to be called asynchronously – Saurabh Jhunjhunwala Jan 18 '21 at 16:34
  • Why call something asynchronously that doesn't return anything useful? – Andreas Jan 18 '21 at 16:36
  • @Andreas you are not being helpful here. OP seems to be confused between `async/await` and `then` syntaxes. I believe he is able to get response asynchronously as he mentioned. Could you justify the close? – Nishant Jan 18 '21 at 16:51
  • "*I want to add them to an array*" - `values` already *is* an array. Just use it. Also I'd recommend `const values = await Promise.all(…);` instead of using `then` syntax. – Bergi Jan 18 '21 at 16:54
  • The answer should work for most of the scenarios. We cannot had-code the response and accept it as a solution. – Saurabh Jhunjhunwala Jan 18 '21 at 16:54
  • @Bergi it is printing a blank array – Saurabh Jhunjhunwala Jan 18 '21 at 16:57
  • 1
    @SaurabhJhunjhunwala What scenarios are you talking about? Where is a response hard-coded? What should it depend on? It's really unclear what you are trying to generalise here. – Bergi Jan 18 '21 at 16:57
  • @SaurabhJhunjhunwala What do you mean by "blank array"? An empty array? Then you didn't pass any promises to `Promise.all`. Or an array of `undefined` values? Then the promises returned by your `get` calls didn't fulfill with anything. – Bergi Jan 18 '21 at 16:58
  • @Bergi "blank array" mean " An empty array", my promise is returning me the expected response, because I am able to log in using ".then (value => {console.log (value) }); – Saurabh Jhunjhunwala Jan 18 '21 at 17:01
  • You cannot use both `await` and a `then` callback that doesn't return anything. Use only `const values = await Promise.all(…); console.log(values); …`. – Bergi Jan 18 '21 at 17:03

1 Answers1

0

This will work

const asynchUploadGrpFiles = async (files) => {
  const [data1, data2, data3] = await Promise.all([
    get('https://httpbin.org/get?a=1'),
    get('https://httpbin.org/get?b=2'),
    get('https://httpbin.org/get?c=3'),
  ])
  const responseDetails = [...data1, ...data2, ...data3]
  console.log(responseDetails.toString())
}

Then to call the function

// Call it somewhere
await asynchUploadGrpFiles(files)
omeanwell
  • 1,847
  • 1
  • 10
  • 16