I’m looking to make 2 generic, reusable functions to process parallel and sequential callouts.
Currently, we use inline await to handle sequential…
export async function init() {
const results1 = await callout1();
const results2 = await callout2();
}
… and something like this to handle parallel.
async function init() {
const records = await this.loadRecords();
}
async function loadRecords(){
const records = getRecords({recordId: this.recordId});
const picklistOptions = getPicklistOptions();
return {
records: await records,
picklistOptions: await picklistOptions
}
}
I’m wanting some generic function we can use anywhere to just pass an array of functions and let it handle the rest for us. I’ve thought about something like this, but I don’t know how I would assign and return the values.
import { asyncParallel, asyncSequential } from 'c/util';
async function init() {
const records = await asyncParallel([getRecords(), getPicklistOptions()]);
const records2 = await asyncSequential([getRecords(), getPicklistOptions()]);
}
async function asyncParallel(arrayFunctions) {
return await Promise.all(arrayFunctions);
}
async function asyncSequential(arrayFunctions) {
// I think this would just run in parallel because the for loop would just through the promise in the array
let ret = [];
for(const func of arrayFunctions){
ret.push(await func);
}
return ret;
}
Although, I feel like the only time you would want to run async functions to get records sequentially would be because you need something from the first records before you’re able to make the second callout. So maybe having a generic async for that isn’t helpful. But, a generic function to handle parallel would be very beneficial.
ThatTreyGuy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.