run

Executes a Task and returns a TaskExecution object representing the execution.

This feature is experimental!

This API is still experimental, so it may change or be removed in future versions. You should not rely on it for production applications.

Signature

value()
forall e, v: (Task e v).() => TaskExecution e v

Documentation

Executes a Task and returns a TaskExecution object representing the execution.

Example:

const { task } = require('folktale/concurrency/task');

let message = '';
const sayHello = task(resolver => {
  message = 'hello';
  resolver.resolve();
});

$ASSERT(message == '');

await sayHello.run().promise();

$ASSERT(message == 'hello');

Properties

Source Code

Defined in source/concurrency/task/_task.js at line 20, column 0
run() {
    let deferred = new Deferred();    // eslint-disable-line prefer-const
    let cleanups      = [];
    let cancellations = [];
    let isCancelled   = false;
    let done          = false;

    deferred.listen({
      onCancelled: _ => {
        done = true;
        isCancelled = true;
        cancellations.forEach(f => f());
        cleanups.forEach(f => f());
        cancellations = [];
        cleanups = [];
      },

      onResolved: _value => {
        done = true;
        cleanups.forEach(f => f());
        cleanups = [];
        cancellations = [];
      },

      onRejected: _reason => {
        done = true;
        cleanups.forEach(f => f());
        cleanups = [];
        cancellations = [];
      }
    });

    const execution = new TaskExecution(this, deferred);

    const resources = this._computation({
      reject:  error => { deferred.reject(error) },
      resolve: value => { deferred.resolve(value) },
      cancel:  _     => { deferred.maybeCancel() },

      get isCancelled() { return isCancelled },
      cleanup(f) {
        if (done) {
          throw new Error('Can\'t attach a cleanup handler after the task is settled.');
        }
        cleanups.push(f);
      },
      onCancelled(f) {
        if (done) {
          throw new Error('Can\'t attach a cancellation handler after the task is settled.');
        }
        cancellations.push(f);
      }
    });

    return execution;
  }
Stability
experimental
Licence
MIT
Module
folktale/concurrency/task/_task
Authors
Copyright
(c) 2013-2017 Quildreen Motta, and CONTRIBUTORS
Authors
  • Quildreen Motta
Maintainers
  • Quildreen Motta <queen@robotlolita.me> (http://robotlolita.me/)