diff --git a/src/util/deferred.ts b/src/util/deferred.ts index c530d552..3f2413d2 100644 --- a/src/util/deferred.ts +++ b/src/util/deferred.ts @@ -3,8 +3,8 @@ export type MaybePromise = T | PromiseLike; /** * Represents the completion of an asynchronous operation that can be completed later. */ -export class Deferred implements PromiseLike { - readonly promise: Promise; +export class Deferred implements PromiseLike { + private readonly promise: Promise; private _resolve: (value: MaybePromise) => void = () => {}; private _reject: (reason?: any) => void = () => {}; diff --git a/test/util/deferred.test.ts b/test/util/deferred.test.ts new file mode 100644 index 00000000..a5474d4b --- /dev/null +++ b/test/util/deferred.test.ts @@ -0,0 +1,52 @@ +import { Deferred } from '../../src/util/deferred'; + +describe('Tests cached status code', () => { + it('test resolve method', () => { + const deferred = new Deferred(); + + expect(deferred).resolves.toBe(1); + deferred.resolve(1); + }); + + it('test reject method', () => { + const deferred = new Deferred(); + + expect(deferred).rejects.toBe(1); + deferred.reject(1); + }); + + it('test then method', () => { + const deferred = new Deferred(); + + deferred.then((data) => { + expect(data).toBe(1); + }); + + deferred.resolve(1); + }); + + it('test catch method', () => { + const deferred = new Deferred(); + + deferred.catch((data) => { + expect(data).toBe(1); + }); + + deferred.resolve(1); + }); + + it('test finally method', () => { + const deferred = new Deferred(); + + let data: number; + deferred.then((d) => { + data = d; + }); + + deferred.finally(() => { + expect(data).toBe(1); + }); + + deferred.resolve(1); + }); +});