deferred.ts 937 B

123456789101112131415161718192021222324252627282930313233343536
  1. /**
  2. * Wrapper class for promise with external resolve and reject.
  3. */
  4. export class Deferred<T> {
  5. /**
  6. * The promise associated with this deferred object.
  7. */
  8. public readonly promise: Promise<T>;
  9. private _resolve: (value: T | PromiseLike<T>) => void;
  10. private _reject: (reason?: any) => void;
  11. /**
  12. * The resolve method of the promise associated with this deferred object.
  13. */
  14. public get resolve() {
  15. return this._resolve;
  16. }
  17. /**
  18. * The reject method of the promise associated with this deferred object.
  19. */
  20. public get reject() {
  21. return this._reject;
  22. }
  23. /**
  24. * Constructor for this deferred object.
  25. */
  26. constructor() {
  27. this.promise = new Promise((resolve: (value: T | PromiseLike<T>) => void, reject) => {
  28. this._resolve = resolve;
  29. this._reject = reject;
  30. });
  31. }
  32. }