Class Task<TValue>

The Task class is used for creating lazy PromiseLike | PromiseLike object that will only execute when awaited or when then method is called. Note the class is immutable.

IMPORT_PATH: "@daiso-tech/core/task"

Type Parameters

  • TValue

Implements

Constructors

Methods

  • The detach method executes the Task without awaiting it.

    Returns void

    import { Task } from "@daiso-tech/core/task";
    import { TimeSpan } from "@daiso-tech/core/time-span" from "@daiso-tech/core/time-span";

    const promise =
    new Task(async () => {
    await Task.delay(TimeSpan.fromSeconds(1));
    // Will be loged after one second
    console.log("Done !");
    });

    promise.detach();

    // Will be logged immediately
    console.log("Hello");
    await Task.delay(TimeSpan.fromSeconds(2));
  • The delay method creates a Task that will be fulfilled after given time.

    Parameters

    • time: ITimeSpan
    • abortSignal: AbortSignal = ...

    Returns ITask<void>

    import { Task } from "@daiso-tech/core/task";
    import { TimeSpan } from "@daiso-tech/core/time-span" from "@daiso-tech/core/time-span";

    console.log("a");
    await Task.delay(TimeSpan.fromSeconds(2));
    console.log("b");
  • The fromCallback is convience method used for wrapping Node js callback functions with a Task.

    Type Parameters

    • TValue

    Parameters

    Returns ITask<TValue>

    import { Task } from "@daiso-tech/core/task";
    import { readFile } from "node:fs";

    const task = Task.fromCallback<Buffer | string>((resolve, reject) => {
    readFile("FILE_PATH", (err, data) => {
    if (err !== null) {
    reject(err);
    return;
    }
    resolve(data);
    });
    });
    const file = await task;
    console.log(file);