Created
February 15, 2025 07:54
-
-
Save mimshins/70b316b88eafa7f612eb10ff6f08c861 to your computer and use it in GitHub Desktop.
An utility function to create a promise and its resolvers.
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
const createPromiseResolvers = <T = void>() => { | |
const noop = () => void 0; | |
let _resolve: (value: T | PromiseLike<T>) => void = noop; | |
let _reject: (reason?: unknown) => void = noop; | |
const createPromise = () => | |
new Promise<T>((res, rej) => { | |
_resolve = res; | |
_reject = rej; | |
}); | |
let _promise = createPromise(); | |
const renew = () => { | |
_promise = createPromise(); | |
}; | |
return { | |
get promise() { | |
return _promise; | |
}, | |
get resolve() { | |
return _resolve; | |
}, | |
get reject() { | |
return _reject; | |
}, | |
renew, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment