-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create deferredPromise & waitPromise
- Loading branch information
1 parent
20fd707
commit 1f9a4bb
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
|
||
}) |