TheEdge
TheEdge

Reputation: 9851

Make a bunch of async subrequests in a cloudflare worker and return once they are all complete?

I want to pass in an array of strings to the Cloudflare worker and then have it loop over those strings and do a GET for each one and then adding the JSON the get returns to a list which is returned by the worker to the caller.

Some pseudo code:

var listOfAjaxResults
foreach someString in arrayOfStrings
{
    //Do AJAX call using someString and add to listOfResults
}

//Wait here until all requests in the loop have completed
//Return response form worker
return listOfAjaxResults

I know how I can make a non blocking request as per this SO post. What I cannot work out is:

  1. How to only return once all the requests are complete from the loop
  2. What kind of thread safe data structure to use so that as each request completes it can safely add its result to the list.

Upvotes: 1

Views: 1475

Answers (2)

T W
T W

Reputation: 6317

Promise.all is way to go, there's even easy to digest example in docs how to use it in Workers: https://developers.cloudflare.com/workers/recipes/aggregating-multiple-requests/

If any of the requests fails awaiting on Promise.all will throw so it could be a good idea to wrap in in try/catch if desired.

Upvotes: 3

Brett
Brett

Reputation: 848

You can use Promise.all, re-using your example:

async function example() {
    let arrayOfStrings = ["a", "b", "c"]

    let promises = []
    for (let str of arrayOfStrings) {
        // a single fetch request, returns a promise
        // NOTE that we don't await!
        let promise = fetch(str)

        promises.push(promise)
    }

    let results = await Promise.all(promises)
    // results is now an array of fetch results for the requests,
    // in the order the promises were provided
    // [fetchResult_a, fetchResult_b, fetchResult_b]

    return results
}

Upvotes: 5

Related Questions