I have 2 arrays of queries, but I'm not sure how many queries I have in each array until runtime. I would like to save the results from each array of queries to a variable (e.g. queries from the 1st array should be saved to the 1st array and queries from the 2nd array should be saved to the 2nd). I was going to use promise.all() but I don't know how to assign only some of the responses to 1 array and some to the other. Something like the following:
async function runQueriesAndGetResponse() {
var queryArray1 = [query, anotherQuery, 3rdQuery,...]
var queryArray2 = [4thQuery, 5thQuery, 6thQuery,...]
return await Promise.all(queryArray1.concat(queryArray2))
}
var [responses1, responses2] = await runQueriesAndGetResponse()
// Doesn't work because responses1 and responses2 will only have the 1st and 2nd response respectively
CodePudding user response:
You can return an array from your function, where each element is the Promise.all
of each of your queryArray
Basically the return value of your function should be
return [await Promise.all(queryArray1), await Promise.all(queryArray2)];