From 1f9a4bbdb6394d1d81f264c6fcbb4ef052bf7111 Mon Sep 17 00:00:00 2001 From: Willy Brauner Date: Thu, 31 Aug 2023 11:24:41 +0200 Subject: [PATCH] Create deferredPromise & waitPromise --- src/index.ts | 3 ++ src/promise/deferredPromise.ts | 38 ++++++++++++++++++ src/promise/waitPromise.ts | 11 ++++++ tests/promise/deferredPromise.test.ts | 56 +++++++++++++++++++++++++++ 4 files changed, 108 insertions(+) create mode 100644 src/promise/deferredPromise.ts create mode 100644 src/promise/waitPromise.ts create mode 100644 tests/promise/deferredPromise.test.ts diff --git a/src/index.ts b/src/index.ts index 378057c..abaecf2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,6 +10,9 @@ export { clamp } from "./math/clamp" export { modulo } from "./math/modulo" export { normalizeValue } from "./math/normalizeValue" export { originalValueFromNormalized } from "./math/normalizeValue" +// promise +export { deferredPromise } from "./promise/deferredPromise" +export { waitPromise } from "./promise/waitPromise" // socials export * from "./social/shareUrls" // strings diff --git a/src/promise/deferredPromise.ts b/src/promise/deferredPromise.ts new file mode 100644 index 0000000..52f6961 --- /dev/null +++ b/src/promise/deferredPromise.ts @@ -0,0 +1,38 @@ +/** + * Create a deferred promise who can be resolved or rejected from anywhere. + * Mainly stolen from https://github.com/willybrauner/deferred-promise + * @credit Willy Brauner https://github.com/willybrauner + * @name deferredPromise + * @return TDeferredPromise + * ex: + * + * const deferred = deferredPromise(); + * + * deferred.promise.then(() => { + * // deferredPromise is resolved! + * }); + * + * // when needed, resolve this promise + * deferred.resolve(); + * + * // or reject it + * deferred.reject(); + * + */ + +export type TDeferredPromise = { + promise: Promise; + resolve: (resolve?: T) => void; + reject: (error?: Error | any) => void; +}; + +export function deferredPromise(): TDeferredPromise { + const deferred: TDeferredPromise | any = {}; + + deferred.promise = new Promise((resolve, reject) => { + deferred.resolve = resolve; + deferred.reject = reject; + }); + + return deferred; +} diff --git a/src/promise/waitPromise.ts b/src/promise/waitPromise.ts new file mode 100644 index 0000000..2fede17 --- /dev/null +++ b/src/promise/waitPromise.ts @@ -0,0 +1,11 @@ +/** + * Returns a promise that resolves after a delay + * @param {number} delay + * + * ex: + * await waitPromise(1000) + * + */ +export function waitPromise(delay: number = 1000): Promise { + return new Promise((resolve) => setTimeout(resolve, delay)) +} diff --git a/tests/promise/deferredPromise.test.ts b/tests/promise/deferredPromise.test.ts new file mode 100644 index 0000000..d20ad02 --- /dev/null +++ b/tests/promise/deferredPromise.test.ts @@ -0,0 +1,56 @@ +import {describe, expect, it} from "vitest" +import { deferredPromise, waitPromise } from "../../src" + +/** + * @credit Willy Brauner https://github.com/willybrauner + * Mainly stolen from https://github.com/willybrauner/deferred-promise/blob/main/test/index.test.ts + */ +describe("deferredPromise", () => { + it('should resolve the deferred promise', async () => { + let resolve = false + let reject = false + const deferred = deferredPromise() + + deferred.promise + .then(() => { + resolve = true + }) + .catch(() => { + reject = true + }) + + expect(resolve).toBe(false) + expect(reject).toBe(false) + + deferred.resolve() + await waitPromise(0) + + expect(resolve).toBe(true) + expect(reject).toBe(false) + }) + + it('should reject the deferred promise', async () => { + + let resolve = false + let reject = false + const deferred = deferredPromise() + + deferred.promise + .then(() => { + resolve = true + }) + .catch(() => { + reject = true + }) + + expect(resolve).toBe(false) + expect(reject).toBe(false) + + deferred.reject() + await waitPromise(0) + + expect(resolve).toBe(false) + expect(reject).toBe(true) + }) + +})