sdk.jobs.runjobs
Home > @matterway/sdk > Jobs > runJobs
Jobs.runJobs() function
A Job is a function that returns a promise. This helper can be used to run multiple jobs concurrently, returning a promise that resolves as an array of all the results.
Signature:
export declare function runJobs<T extends JobList>(ctx: Context, jobs: T, options?: RunJobsOptions): JobResults<T>;
Parameters
Parameter | Type | Description |
---|---|---|
ctx | Context | The context to run the jobs in. |
jobs | T | An array of jobs to run. |
options | RunJobsOptions | (Optional) Options for running the jobs. |
Returns:
JobResults<T>
An array of results, in the same order as the jobs array.
Remarks
- To run all jobs without rejecting the promise if one of them fails, use try/catch inside the job handler.
Example
async function step(ctx: Context) {
const jobs = [
{title: 'job 1', handler: () => Promise.resolve(1)},
{title: 'job 2', handler: () => Promise.resolve(2)},
{title: 'job 3', handler: () => Promise.resolve(3)},
];
const options = {concurrency: jobs.length, onUpdate: console.log};
const results = await runJobs(ctx, jobs, options);
return results;
}
const ctx = {};
step(ctx).then(results => console.log(results));