No output to JS Query

Hi there, the retool console logs the output of my JS Query beautifully, but no output is ever recorded. Possibly an ignorant JS error in here. But It's confounding me. Help appreciated!

My objective: get a bunch of different stripe customer IDs from our database (don't ask me why stripe assigns a single customer with the same email address 4 different "customer IDS"), and then loop through them all, using the stripe API to fetch all subscriptions for each.

I'm expecting that return resultsArray; should make the data shown in the console available as output (in the "Output" panel in retool for this query). But it doesn't.

All related queries seem to work, as evidenced by the expected output in the console.log() output (pasted below).

This is the JS code I'm using to glue together the two separate queries (postgres to get the stripe customer IDs, and the stripe API). It appears to work fine except for the missing output:

// Initialize an array to store the results
let resultsArray = [];

// Initialize a counter for completed queries
let completedQueries = 0;

// Function to handle the final output
function handleFinalOutput() {
  if (completedQueries === stripe_ids.data.length) {
    // All queries have completed
    // Now, you can bind `resultsArray` to a Retool component or use it as needed
    console.log("Final results:", resultsArray);

    return resultsArray;
  }
}

// Loop through each item in the array
stripe_ids.data.forEach(item => {

  const stripeId = item.stripe_id;

  subscriptions.trigger({
    additionalScope: { stripe_id: stripeId },
    onSuccess: (data) => {
      // Push the successful result into the array
      resultsArray.push(data);

      // Increment the counter and check if all queries are done
      completedQueries++;
      handleFinalOutput();
    },
    onFailure: (error) => {
      // Log the error and consider how to handle failed queries
      console.error("Lookup failed for stripe_id:", stripeId, "Error:", error);

      // Increment the counter even in case of failure
      completedQueries++;
      handleFinalOutput();
    }
  });
});

Console output (couldn't find a convenient way to export):

Hello, you can access the JS query output as

yourQueryID.data

query1 output:

query2 accessing query1 output: