ts-data-forge
    Preparing search index...

    Function createPromise

    • Creates a Promise that wraps the result in a Result type for type-safe error handling. This function is an alternative to new Promise(executor) that provides enhanced type safety by returning a Result type instead of throwing exceptions.

      Type Parameters

      • S

        The type of successful value

      • E

        The type of error value

      Parameters

      • executor: (resolve: (value: S) => void, reject: (reason?: E) => void) => void

        Function that takes resolve and reject callbacks

      Returns Promise<Result<S, E>>

      A Promise that resolves to a Result containing either success or error

      // Create a promise that resolves successfully
      const successPromise = createPromise<number, string>((resolve) => {
      setTimeout(() => {
      resolve(42);
      }, 0);
      });

      const successResult = await successPromise;
      assert.ok(Result.isOk(successResult));
      if (Result.isOk(successResult)) {
      assert(successResult.value === 42);
      }

      // Create a promise that rejects with an error
      const errorPromise = createPromise<number, string>((_, reject) => {
      setTimeout(() => {
      reject('Something went wrong');
      }, 0);
      });

      const errorResult = await errorPromise;
      assert.ok(Result.isErr(errorResult));
      if (Result.isErr(errorResult)) {
      assert(errorResult.value === 'Something went wrong');
      }