0

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.

1
  • What is your question? You say you don't know how to assign and return the results, but it looks like you're doing it when you write const records = await asyncParallel(...) Commented Jul 1, 2024 at 20:28

1 Answer 1

0

For parallel use:

const asyncParallel = (promises) => Promise.allSettled(promises);

If you will use Promise.all(promises) it will be rejected if any of the promises will be rejected. Instead, you just need to use Promise.allSettled(promises), this will return both succeeded and failed results.

For sequential use: Sequential promises often depends on previous one, so you probably don't need to function, you can just chain them using .then or awaiting sequentially like this:

const result1 = await getPromise1();
const result2 = await  getPromise2(result1);

But, if you want to just run them sequentially no matter if there will be an error, you can write a function like this:

const asyncSequential = async (promises) => {
    const results = [];
    for (const promise of promises) {
        const result = {};
        try {
            result.value = await promise;
            result.status = 'fulfilled';
        }
        catch {
            result.status = 'rejected';
        }
        results.push(result);
    }
   
    return results;
}

This function is made to mimic Promise.allSettled result but in parallel.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.