2023-06-06 22:45:16 +00:00
|
|
|
//@ts-check
|
2023-06-13 16:50:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @template {any} T
|
|
|
|
* @typedef {{
|
|
|
|
* resolve: (value: T) => void
|
|
|
|
* reject: (reason?: any) => void
|
|
|
|
* } & Promise<T>} DeferredPromise<T> A promise that can be resolved externally
|
|
|
|
*/
|
2023-06-06 22:45:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a promise that can be resolved externally.
|
|
|
|
* @template {any} DeferredPromiseType
|
|
|
|
* @returns {DeferredPromise<DeferredPromiseType>}
|
|
|
|
*/
|
|
|
|
export const deferredPromise = () => {
|
|
|
|
/** @type {(value: DeferredPromiseType) => void} */
|
|
|
|
let resolve
|
|
|
|
/** @type {(reason?: any) => void} */
|
|
|
|
let reject;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {Promise<DeferredPromiseType>}
|
|
|
|
*/
|
|
|
|
const promise = new Promise((_resolve, _reject) => {
|
|
|
|
resolve = _resolve;
|
|
|
|
reject = _reject;
|
|
|
|
});
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
return Object.assign(promise, {resolve, reject});
|
|
|
|
}
|
|
|
|
|
2023-06-07 16:51:18 +00:00
|
|
|
export default deferredPromise
|