Matterway
    Preparing search index...

    Function runJobsSequentially

    • A Job is a function that returns a promise. This helper can be used to run jobs sequentially, passing the result of a job to the next one as input, returning a promise that resolves as an array of all the results.

      Type Parameters

      • T extends JobList

      Parameters

      • ctx: Context

        The context to run the jobs in.

      • jobs: T

        An array of jobs to run.

      • Optionaloptions: RunJobsSequentiallyOptions

        Options for running the jobs.

      Returns JobResults<T>

      An array of results, in the same order as the jobs array.

      async function step(ctx: Context) {
      const jobs = [
      {title: 'job 1', handler: () => Promise.resolve(1)},
      {title: 'job 2', handler: (ctx:Context, result:number) => Promise.resolve(result * 2)},
      {title: 'job 3', handler: (ctx:Context, result:number) => Promise.resolve(result * 3)},
      ];
      const options = {onUpdate: console.log};
      const results = await runJobsSequentially(ctx, jobs, options);
      return results;
      }
      const ctx = {};
      step(ctx).then(results => console.log(results));
      • To run all jobs without rejecting the promise if one of them fails, use try/catch inside the job handler.