Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Deferred<T>

A Deferred that can be completed via public functions resolve or reject. Unlike the Promise class, this class does not require the new keyword.

example
"nodejs";
const { Deferred } = require('lang');
async function main() {
    console.log(await add(1, 2));
}
main();

function add(a, b) {
    const d = new Deferred();
    setTimeout(() => {
        d.resolve(a + b);
    }, 1000);
    return d.promise();
}

Type parameters

  • T

Hierarchy

  • Deferred

Index

Constructors

Accessors

Methods

Constructors

constructor

  • new Deferred<T>(keepRunning?: boolean): Deferred<T>
  • Type parameters

    • T

    Parameters

    • keepRunning: boolean = true

    Returns Deferred<T>

Accessors

value

  • get value(): undefined | T
  • Gets the value of the Deferred, if it is pending or rejected, then returns undefined.

    Returns undefined | T

Methods

promise

  • promise(): Promise<T>
  • Returns Promise<T>

reject

  • reject(reason?: any): boolean
  • Sets the state of the Promise to rejected using the given reason or exception. If the Promise is already fulfilled or rejected, returns false.

    Parameters

    • Optional reason: any

    Returns boolean

resolve

  • resolve(value: T): boolean
  • Sets the state of the Promise to fulfilled using the given value. If the Promise is already fulfilled or rejected, returns false.

    Parameters

    • value: T

    Returns boolean