Task

Description

Run a node script in a separate process.

Used by the fuzzy-finder and find in project.

For a real-world example, see the scan-handler and the instantiation of the task.

Examples

In your package code:

const {Task} = require('atom');

let task = Task.once('/path/to/task-file.js', parameter1, parameter2, function() {
  console.log('task has finished');
});

task.on('some-event-from-the-task', (data) => {
  console.log(data.someString); // prints 'yep this is it'
});

In '/path/to/task-file.js':

module.exports = function(parameter1, parameter2) {
  // Indicates that this task will be async.
  // Call the `callback` to finish the task
  const callback = this.async();
  emit('some-event-from-the-task', {
    someString: 'yep this is it'
  });
  return callback();
};

API documentation

All methods

::constructor(taskPath)

Creates a task. You should probably use Task.once

Argument Description
taskPath

The String path to the CoffeeScript/JavaScript file that exports a single Function to execute.

::start(args, callback)

Starts the task.

Throws an error if this task has already been terminated or if sending a message to the child process fails.

Argument Description
args

The arguments to pass to the function exported by this task's script.

callback optional

A Function to call when the task completes.

::send(message)

Send message to the task.

Throws an error if this task has already been terminated or if sending a message to the child process fails.

Argument Description
message

The message to send to the task.

::on(eventName, callback)

Call a function when an event is emitted by the child process

Argument Description
eventName

The String name of the event to handle.

callback

The Function to call when the event is emitted.

Return values
  • Returns a Disposable that can be used to stop listening for the event.

::cancel()

Cancel the running task and emit an event if it was canceled.

Return values
  • Returns a Boolean indicating whether the task was terminated.