Created
November 14, 2024 10:00
-
-
Save erdesigns-eu/36c26adf21241e508b6db2764d7e2633 to your computer and use it in GitHub Desktop.
Utility function to create a promise, and return the promise + reject + resolve
This file contains hidden or 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
type ResolvablePromise<Value = void> = { | |
promise: Promise<Value>; | |
resolve: (value: Value | PromiseLike<Value>) => void; | |
reject: (reason?: any) => void; | |
}; | |
export function makeResolvablePromise<Value = void>(): ResolvablePromise<Value> { | |
let resolveFunction: (value: Value | PromiseLike<Value>) => void; | |
let rejectFunction: (reason?: any) => void; | |
// Create a promise and store its resolve and reject functions | |
const promise = new Promise<Value>((resolve, reject) => { | |
resolveFunction = resolve; | |
rejectFunction = reject; | |
}); | |
// Ensure resolve and reject functions are initialized | |
if (!resolveFunction || !rejectFunction) { | |
throw new Error("Failed to initialize resolve and reject functions"); | |
} | |
return { | |
promise, | |
resolve: resolveFunction, | |
reject: rejectFunction, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment