tiny-future
    Preparing search index...

    Class Future<T>

    A tiny way to make Promise more convenient to use without any dependencies.

    Create a new Future which wraps a new Promise, allowing you to resolve or reject it from outside the executor.

    Inspired by C# TaskCompletionSource.

    const future = new Future<number>();
    asyncFunc(() => {
    future.resolve(0);
    });
    return future.promise;
    const future = new Future<string>();
    setTimeout(() => future.resolve('done'), 1000);
    const result = await future.promise;
    console.log(result); // 'done'
    const future = new Future<void>();
    future.reject(new Error('something went wrong'));
    await future.promise.catch(err => console.error(err));

    Type Parameters

    • T

      The type of the value that the Promise will resolve to.

    Index

    Constructors

    Properties

    Constructors

    • Creates a new Future instance.

      Uses Promise.withResolvers() if available (ES2024+), otherwise falls back to manual implementation for compatibility with older environments.

      Type Parameters

      • T

        The type of the value that the Promise will resolve to.

      Returns Future<T>

    Properties

    promise: Promise<T>

    The Promise instance created by the Future.

    Use this to await the result or attach .then() / .catch() handlers.

    const future = new Future<number>();
    future.promise.then(value => console.log(value));
    future.resolve(42); // logs: 42
    reject: (reason?: unknown) => void

    Rejects the Promise created by the Future.

    Type Declaration

      • (reason?: unknown): void
      • Parameters

        • Optionalreason: unknown

          The reason for rejecting the Promise. Typically an Error object.

        Returns void

    const future = new Future<string>();
    future.reject(new Error('something went wrong'));
    resolve: (value: T | PromiseLike<T>) => void

    Resolves the Promise created by the Future.

    Type Declaration

      • (value: T | PromiseLike<T>): void
      • Parameters

        • value: T | PromiseLike<T>

          The value to resolve the Promise with, or a Promise that resolves to the value.

        Returns void

    const future = new Future<string>();
    future.resolve('success');
    // or with a Promise
    future.resolve(Promise.resolve('success'));