All files / src/impl ExposedPromise.impl.ts

100% Statements 8/8
100% Branches 0/0
100% Functions 5/5
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38                12x 7x               7x               7x       7x   7x 7x       7x  
import { ExposedPromise } from "./ExposedPromise";
 
/** 
 * Create a new ExposedPromise
 *
 * @return the new ExposedPromise
 * @param <T> the type of promise value
 */
export function create<T>(): ExposedPromise<T> {
  return ExposedPromiseImpl.internalCreate<T>();
}
 
// ---- Implementation details below ----
 
class ExposedPromiseImpl<T> implements ExposedPromise<T> {
 
  getPromise(): Promise<T> {
    return this.#promise;
  }
 
  resolve!: (value: T | PromiseLike<T>) => void;
 
  reject!: (reason?: unknown) => void;
 
  static internalCreate<T>(): ExposedPromise<T> {
    return new ExposedPromiseImpl<T>()
  }
 
  private constructor() {
    this.#promise = new Promise<T>((resolve, reject) => {
      // Capture the resolve and reject functions from the Promise constructor
      this.resolve = resolve;
      this.reject = reject;
    });
    // The resolve and reject methods can now be called from outside the constructor scope
  }
  readonly #promise: Promise<T>;
}