How to run a REST API/Query for each item in an array and return all results together

Similar to https://docs.retool.com/docs/scripting-retool#section-triggering-a-query-for-each-item-in-an-array, this setup using Async JS promises will make the .data property of this query include ALL of the results of the triggered queries.

Using a Run JS Code type query, we will trigger another query for each item in an array, and after the last is completed return all of the results together in the jsCodeQueryName.data value.

Where get_assignment is a REST Api query, taking an input of {{assignment_id}} , const promises returns a mapping function of each result of the API where we pass in ID as additionalScope.

image

You could then reference the results inside a calculated column of a table for example using {{jsQueryName.data[i]}} in the calculation to have each row display the result.

NOTE: If you pass in any null values to the .map method, this code will return a “Failed to execute ‘postMessage’ on ‘Window’: # could not be cloned.” error. You’ll need to filter out or replace any null values in the array before then.

5 Likes