2

I have an array like this:

var subscriptions = ['sub_1234', 'sub_5678', 'sub_8493'];

In my real application the array usually has about 800 subscription ids.

Currently I have a loop like this:

var subscriptionData = [];
for(var i in subscriptions) {
  subscriptionID = subscriptions[i];
  var data = await stripe.subscriptions.retrieve(subscriptionID);
  subscriptionData.push(data);
}

The purpose of the loop is to extrapolate the subscription ids to find out all of the information related to that id (payment amounts, invoices, etc.)

The problem is with 600 subscriptions it can take 20-30 minutes to go through all of that data.

Is there away to run the for loop in parallel so that it pulls all of the stripe subscriptions at once and pushes the data onto the array? Then continues?

I was looking at this library: https://github.com/caolan/async

But I couldn't figure out how to get it to do it if you don't know how long the array is (the array can be of variable length)

Jordash
  • 2,926
  • 8
  • 38
  • 77
  • 1
    you should probably re-think how you're handling that many requests. Might want to send the API 1 request with all the ID's instead of 1 at a time – Derek Pollard Sep 10 '18 at 19:37
  • @Derek I was thinking that but couldn't figure out how to do it in Stripe, it only allows grabbing the details of 1 subscription at a time. – Jordash Sep 10 '18 at 19:39
  • 1
    You can retrieve 10 or 20 subscriptions at the same time and `await` for them using `Promise.all` – zeynel Sep 10 '18 at 19:57
  • 1
    There's a difference between async and parallel. To run anything in true parallel with Node, you need to use [child processes](https://nodejs.org/api/child_process.html). However, you can use `Promise.all` to launch simultaneous API calls like fxgx suggested, if that's what you need – dpopp07 Sep 10 '18 at 20:31
  • @dpopp07 Can you provide examples using an array like the example? I can't find any Promise.all() examples with an array like that. – Jordash Sep 10 '18 at 23:53
  • @fxgx Can you provide any examples iterating through an entire array? – Jordash Sep 11 '18 at 00:21
  • @Jordash what @capcap posted is correct. `Promise.all` can have some caveats that are good to know about, I recommend [this thread](https://stackoverflow.com/questions/30362733/handling-errors-in-promise-all) to learn more about it. – dpopp07 Sep 11 '18 at 14:44

1 Answers1

4
const subscriptions = ['sub_1234', 'sub_5678', 'sub_8493'];


async function customFunction(subscriptions) {

  const getSubscription = subscriptions.map((item) => {
    return stripe.subscriptions.retrieve(item);
  });

  const subscriptionData = await Promise.all(getSubscription);

  return subscriptionData; // returns an array
};


// call the function and pass parameter value

customFunction(subscriptions);
capcap
  • 41
  • 2
  • Is there any way to run a request like this in chunks of 10 at a time? I keep getting errors related to too many concurrent requests. – Jordash Sep 19 '18 at 21:29
  • You can try using parallelLimit https://caolan.github.io/async/v3/docs.html#parallelLimit – saurabh Dec 22 '20 at 05:47