Options
All
  • Public
  • Public/Protected
  • All
Menu

Encapsulates the business logic for a single action within a multi-step asynchronous process.

see

action factory method

Hierarchy

  • Record<string, any>
    • Action

Index

Properties

name: string

The name of the process action. Should be unique within a given process instance.

readonly

Methods

  • execute(): any
  • Performs the bulk of the action's business logic. The value returned from this method (or the resolved value of the Promise returned by this method) will be assigned to the results map automatically.

    If you return a Promise, dependent actions will not be run until the Promise has resolved. If the Promise rejects or if the execute method throws an Error, the action's retry method will be run. If that method throws or rejects, the entire process will be aborted.

    example
    import { someAsyncOperation } from '../data';

    const loadData = process.action('load', {
    async execute() {
    return await someAsyncOperation(...this.args);
    }
    });

    Returns any

    Value (or a Promise that resolves to a value) that will be stored in the results collection passed to other actions through context, and provided as the overall process Promise resolve value.

  • failure(error: Error): void
  • This method runs if any action of the process fails (even if this action was not previous executed). It provides a cross-cutting way to respond to errors caused by other actions.

    example
    import { tracker } from '~/tracking';

    const logger = process.action('log process failure', {
    failure(err) {
    tracker.error(err);
    }
    });

    Parameters

    • error: Error

      The Error that failed the process.

    Returns void

  • init(): void
  • Runs once per process invocation. Can be used to initialize local variables or set up starting conditions.

    example
    const multiply = process.action('multiply', {
    factor: 2,
    init() {
    // NOTE: args are passed to the
    // process' start function
    const [factor, _] = this.args;
    this.factor = factor || 2;
    },
    execute() {
    const [_, operand] = this.args;
    return operand * this.factor;
    }
    });

    Returns void

  • retry(error?: Error): any
  • This method is invoked if the execute method throws or returns a rejected Promise. If this method also throws or rejects (which is the default behavior) then the entire process will be aborted.

    example
    import { someAsyncOperation } from '../data';

    const loadData = process.action('load', {
    errorCount: 0,
    init() {
    this.errorCount = 0;
    },
    async execute() {
    return await someAsyncOperation(...this.args);
    },
    retry(err) {
    err.errorCount = ++this.errorCount;
    if (this.errorCount < 3)
    return Promise.resolve(); // try again
    return Promise.reject(); // do not retry
    }
    });

    Parameters

    • Optional error: Error

      The Error raised by the execute method or returned as the rejection reason of that method's Promise.

    Returns any

    Rejected promise to abort the process (default behavior). Any other return value will cause the action's execute method to re-run. If a Promise is returned, execute will not run until and unless the promise resolves.

  • rollback(): void
  • This method is invoked if the action ran but the process was aborted. You can use this opportunity to undo any behaviors performed in the execute method.

    example
    import { someAsyncOperation } from '../data';

    const loadData = process.action('load', {
    store: stores.localStore(),
    async execute() {
    const result = await someAsyncOperation(...this.args);
    await this.store.set('my data cache', result);
    return result;
    },
    async rollback() {
    await this.store.delete('my data cache');
    }
    });

    Returns void

  • success(): void
  • This method runs if and when the entire process resolves. It provides a cross-cutting way to respond to the overall success of a complex process.

    example
    import { tracker } from '~/tracking';

    const logger = process.action('log process sucess', {
    success() {
    tracker.event(`${this.process} successful`, this.results);
    }
    });

    Returns void