Reputation: 9851
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:
Upvotes: 1
Views: 1475
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
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