Skip to content

Tasks

The Tasks module can be used to paralellize parts of a flow. This is useful for e.g. doing concurrent http requests or running background tasks. It is not intended for use with field-operations i.e. interacting with a host applications UI since this interaction cannot be parallelized. Furthermore you should not display dialogs in parallelized tasks as they can block the calling flow.

Run

Use the run method to start a new task.

Parameters

  • fun a function to run in parallel

Returns

Example

Run some tasks and wait for the result.

javascript
var t = Task.run(
  function() { 
    var i = 0;
    while (i<1000) {
      i = i + 1;
    }
    return i;
  });

// Wait for t to complete or 1000ms to elapse
if (t.wait(1000)) {
  // Access the result
  if (t.done && !t.failed) {
    Debug.showDialog("It completed with result="+t.result);
  } else if (t.failed) {
    // only access t.error if t.failed == true
    Debug.showDialog("Took too long or errored? "+t.error !== null);
  }
} else {
  // 1 sec elapsed without the task completing
}

Run a task and execute a function when the task is done.

javascript
Task.run(...).then(function(result){ 
  // do something with the result of the task
});

Wait for all tasks to complete

This is used to wait until all the tasks given as arguments complete or given milliseconds elapse.

Parameters

  • tasks - an [array of tasks or javascript functions] to run asynchronously (and then wait for)
  • timeout [int] denoting the max number of milliseconds to wait for the tasks to complete

Returns

A [bool] indicating wether or not all tasks completed.

Example

javascript
var t = Task.run(function() { ... });
var tasks = [Task.run(function() { ... }), function() { ... }, t];

// Wait for tasks to complete or 1000ms to elapse
if (Task.waitAll(tasks, 1000)) {
  for (var i=0; i<tasks.length; i++) {
    Debug.showDialog("Task "+i+" resulted in "+tasks[i].result);
  }
  Debug.showDialog("It completed!");
} else {
  Debug.showDialog("Took too long");
}

Wait for any tasks to complete

This is used to wait until one of the tasks given as arguments completes or given milliseconds elapse.

Parameters

  • tasks - an [array of tasks or javascript functions] to run asynchronously (and then wait for one of)
  • timeout [int] denoting the max number of milliseconds to wait for any of the task to complete

Returns

An [int] denoting the index of the first task to complete or -1 if no tasks complete within given deadline.

Example

javascript
var t = Task.run(function() { ... });
var tasks = [Task.run(function() { ... }), function() { ... }, t];

// Wait for tasks to complete or 1000ms to elapse
var idx = Task.waitAny(tasks, 1000);
if(idx > 0) {
  Debug.showDialog("We have a winner: "+idx);
} else {
  Debug.showDialog("Took too long. Everybody lost.");
}

JavaScript Task

A javascript representation of a .NET task. It has 2 methods; wait(milliseconds) which can be used to wait for the task to complete or the given milliseconds to elapse, whichever comes first and then(func) which can be used to run a function when the task completes.

For an example see the Run method on the Task module.