File

projects/wms-framework/src/lib/baseframework/Task.ts

Description

Task class

Index

Properties
Methods
Accessors

Constructor

constructor()

Properties

Private _exception
Type : Exception
Default value : new Exception()
Private _reject
Type : Function
Private _resolve
Type : Function
Private fate
Type : "resolved" | "unresolved"
Public promise
Type : Promise<T>
Private state
Type : "pending" | "fulfilled" | "rejected"

Methods

isFulfilled
isFulfilled()
Returns : boolean
isPending
isPending()
Returns : boolean
isRejected
isRejected()
Returns : boolean
isResolved
isResolved()
Returns : boolean
reject
reject(reason?: any)
Parameters :
Name Type Optional
reason any Yes
Returns : void
resolve
resolve(value?: any)
Parameters :
Name Type Optional
value any Yes
Returns : void

Accessors

Exception
getException()
import { Exception } from './Exceptions';

/**
 * Task class
 *
 * @export
 * @class Task
 * @template T
 * @wType System.Threading.Tasks.Task`1
 * @wNetSupport
 */
export class Task<T> {
  public promise: Promise<T>;

  private fate: 'resolved' | 'unresolved';
  private state: 'pending' | 'fulfilled' | 'rejected';

  private _resolve: Function;
  private _reject: Function;
  private _exception: Exception = new Exception();

  constructor() {
    this.state = 'pending';
    this.fate = 'unresolved';
    this.promise = new Promise((resolve, reject) => {
      this._resolve = resolve;
      this._reject = reject;
    });
    this.promise.then(
      () => (this.state = 'fulfilled'),
      (reason?: any) => {
        this._exception = reason?.Message
          ? new Exception(reason?.Message)
          : new Exception('Unknown');
        this.state = 'rejected';
      }
    );
  }

  resolve(value?: any) {
    if (this.fate === 'resolved') {
      throw 'Deferred cannot be resolved twice';
    }
    this.fate = 'resolved';
    this._resolve(value);
  }

  reject(reason?: any) {
    if (this.fate === 'resolved') {
      throw 'Deferred cannot be resolved twice';
    }
    this.fate = 'resolved';
    this._exception = reason?.Message
      ? new Exception(reason?.Message)
      : new Exception('Unknown');
    this._reject(reason);
  }

  isResolved() {
    return this.fate === 'resolved';
  }

  isPending() {
    return this.state === 'pending';
  }

  isFulfilled() {
    return this.state === 'fulfilled';
  }

  isRejected() {
    return this.state === 'rejected';
  }

  public get Exception(): Exception {
    return this._exception;
  }
}

result-matching ""

    No results matching ""