Skip to content

Commit

Permalink
Create deferredPromise & waitPromise
Browse files Browse the repository at this point in the history
  • Loading branch information
willybrauner committed Aug 31, 2023
1 parent 20fd707 commit 1f9a4bb
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions src/promise/deferredPromise.ts
Original file line number Diff line number Diff line change
@@ -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<T = any> = {
promise: Promise<T>;
resolve: (resolve?: T) => void;
reject: (error?: Error | any) => void;
};

export function deferredPromise<T = any>(): TDeferredPromise<T> {
const deferred: TDeferredPromise<T> | any = {};

deferred.promise = new Promise((resolve, reject) => {
deferred.resolve = resolve;
deferred.reject = reject;
});

return deferred;
}
11 changes: 11 additions & 0 deletions src/promise/waitPromise.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
return new Promise((resolve) => setTimeout(resolve, delay))
}
56 changes: 56 additions & 0 deletions tests/promise/deferredPromise.test.ts
Original file line number Diff line number Diff line change
@@ -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<void>()

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<void>()

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)
})

})

0 comments on commit 1f9a4bb

Please sign in to comment.