workers

require(["esri/core/workers"], function(workers) { /* code goes here */ });
Object: esri/core/workers
Since: ArcGIS API for JavaScript 4.2

This module is a utility framework that simplifies the use of Web Workers in the ArcGIS API for JavaScript.

The workers framework takes advantage of multi-core CPUs to perform computationally expensive tasks in a background thread without interfering with the user interface. This framework loads a script and provides a calling function in the main thread using the Connection class. The Connection can then be used to offload jobs asynchronously from the main thread to workers. The workers framework returns a Promise once the job is completed.

The following diagram shows the workflow.

workers-basic

Known Limitations

Method Overview

NameReturn TypeSummary
Promise

Opens a connection to workers and loads a script with the workers framework.

more details
more details

Method Details

open(client, modulePath){Promise}static

Opens a connection to workers and loads a script with the workers framework.

Parameters:
client Object

The instance of the client which will be using the Connection.

modulePath String

A fully qualified URL to a script to execute with the workers framework.

Returns:
TypeDescription
PromiseResolves to an instance of Connection.
See also:
Example:
// Set worker's loaderConfig to set up
// a path to folder called workersScripts
esriConfig.workers.loaderConfig = {
 paths: {
   workerScripts: window.location.href.replace(/\/[^/]+$/, "/workerScripts"),
 }
};

// Load workerScripts/Calculator.js in the workers framework
// and invoke its "max" method
workers.open(this, "workerScripts/Calculator")
  .then(function(connection) {
    return connection.invoke("max", {
     numbers: [0, 1, 2, 3, 4]
    });
  })
  .then(function(result) {
    console.log(result);
  });

//*********************************************************
// module: workerScripts/Calculator.js
//*********************************************************
define(["esri/core/promiseUtils"], function(promiseUtils) {

  // when loaded, the workers framework will create new `Calculator`
  var Calculator = function Calculator() {};

  // any method on "Calculator" module can be invoked
  // from the main thread
  Calculator.prototype.max = function(data) {
    return promiseUtils.resolve({
     data: Math.max.apply(null, data.numbers)
    });
  }
  return Calculator;
});

API Reference search results

NameTypeModule

There were no match results from your search criteria.