do

compute.do(work, args)
compute.do(n, work, args)

Returns a JobHandle which for each invocation of the work function, work will recieve x arguments, where the first argument is a unique number >= 0 and <n, and the other x-1 arguments are from the array arguments.

Two forms of this function are given. The first is equivalent to the second when n==1.

Arguments:
  • n (number) – The number of times to run the work function.

  • work (function|string|URL|DcpURL) – Work function that will be run on each slice.

  • args (array-like object) – Array-like object which contains arguments that are passed to the work function (along with the slice index).

Note

compute.do() is useful when the input set is 1...n, when the input to each slice does not matter for the work done in that slice, or if the job has only one slice. In almost all other cases, compute.for() is more versatile.

Forms

Form 1

Creates a job that executes the work function once.

do(work, args = []) => Job

Arguments:

  • work (function|string|URL|DcpURL) - The work function to run. Can be a function, string literal, or a URL that points to a function that’s fetched by the worker.

  • args (Array-Like Object) - Array-like object which contains arguments passed to the work function (along with the slice index).

const { init } = require('dcp-client');

init().then((dcp) => {
  const compute = require('dcp/compute');
  // or
  // const { compute } = dcp;
  const job = compute.do(
    (i, arg1, arg2) => {
      progress('100%');
      return i * arg1 * arg2;
    },
    [2, 3],
  );
});

Form 2

Creates a job that executes the work function on the values 1...n.

do(n, work, args = []) => Job

Arguments:

  • n (number) - Number of times to run the work function

  • work (function|string|URL|DcpURL) - The work function to run.

  • args (Array-Like Object) - Array-like object which contains arguments passed to the work function (along with the slice index). Can be a function string literal or a URL that points to a function fetched by the worker.

Example:

const { init } = require('dcp-client');

init().then((dcp) => {
  const compute = require('dcp/compute');
  // or
  // const { compute } = dcp;
  const job = compute.do(
    5,
    (i, arg1, arg2) => {
      progress('100%');
      return i * arg1 * arg2;
    },
    [2, 3],
  );
});