-1

I'm trying to make XMLRPC requests for each server and then combine all data into total variable. Thing is that I get "pending" as a return and with setTimeout is "fullfiled" I mean I know that first one is still processing and another one is that it succeded but how to get actual arrays? What am I doing wrong that it doesn't return values?

const servers = ["server1", "server2];
      try {
        const total = servers.map(async server => {
          const res = await sendXMLRPCRequest(null, server, Commands.get_all_devices_id)
          res.map(unit => {
            return {
              "id": unit,
              "server": server,
              "modules": []
            }
          })
        })
        setTimeout(() => console.log(total), 5000)
        console.log(total)


      }
      catch (err) {
        console.log(err)
      }
Jacki
  • 632
  • 3
  • 14
  • 26
  • 1
    You'll want to await all those promises with `Promise.all()`… – deceze Jun 22 '20 at 09:38
  • Duplicate of [How can I wait for set of asynchronous callback functions?](https://stackoverflow.com/questions/10004112/how-can-i-wait-for-set-of-asynchronous-callback-functions) – Guy Incognito Jun 22 '20 at 09:43

1 Answers1

1

Try this

const total = Promise.all ( servers.map (async server => {
    const res = await sendXMLRP.....;
    return res.map (unit => { // whatever you return 
    })
}))

Check out Promise.All documentation

Ishan Joshi
  • 116
  • 6