Skip to content

Function: runJobs()

ts
function runJobs<T>(
   ctx: Context, 
   jobs: T, 
options?: RunJobsOptions): JobResults<T>;

Defined in: src/jobs/runJobs.ts:262

Run multiple jobs concurrently and return all results.

Type Parameters

Type Parameter
T extends JobList

Parameters

ParameterTypeDescription
ctxContextContext to run the jobs in.
jobsTArray of jobs to run.
options?RunJobsOptionsConcurrency and callback options.

Returns

JobResults<T>

Array of results in the same order as the jobs.

Example

Run jobs concurrently

ts
const jobs = [
  {
    title: 'Fetch weather',
    handler: async (ctx) => {
      await wait(2000);
      return {temp: 22, city: 'Berlin'};
    },
  },
  {
    title: 'Fetch news',
    handler: async (ctx) => {
      await wait(1000);
      return {headline: 'Cat rides skateboard'};
    },
  },
];

const options = {concurrency: jobs.length, onUpdate: console.log};
const results = await runJobs(ctx, jobs, options);
console.log(results);

Run jobs sequentially with concurrency 1

ts
const jobs = [
  {title: 'Step 1', handler: async () => 'done-1'},
  {title: 'Step 2', handler: async () => 'done-2'},
  {title: 'Step 3', handler: async () => 'done-3'},
];

const results = await runJobs(ctx, jobs, {concurrency: 1});
console.log(results);

Remarks

To keep going when a job fails, wrap the handler body in try/catch.

Matterway Assistant SDK Documentation