swap

Inverts the state of a Task. That is, turns successful tasks into failed ones, and failed tasks into successful ones.

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).() => Task v e

Documentation

Inverts the state of a Task. That is, turns successful tasks into failed ones, and failed tasks into successful ones.

Example:

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

try {
  const result1 = await of(1).swap().run().promise();
  throw 'never happens';
} catch (error) {
  $ASSERT(error == 1);
}

const result2 = await rejected(1).swap().run().promise();
$ASSERT(result2 == 1);

Properties

Source Code

Defined in source/concurrency/task/_task.js at line 20, column 0
swap() {
    return new Task(resolver => {
      let execution = this.run();   // eslint-disable-line prefer-const
      resolver.onCancelled(() => execution.cancel());

      execution.listen({
        onCancelled: resolver.cancel,
        onRejected:  resolver.resolve,
        onResolved:  resolver.reject
      });
    });
  }
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/)