diff options
Diffstat (limited to 'remote/test/puppeteer/src/util/DeferredPromise.ts')
-rw-r--r-- | remote/test/puppeteer/src/util/DeferredPromise.ts | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/remote/test/puppeteer/src/util/DeferredPromise.ts b/remote/test/puppeteer/src/util/DeferredPromise.ts new file mode 100644 index 0000000000..2f21087ba8 --- /dev/null +++ b/remote/test/puppeteer/src/util/DeferredPromise.ts @@ -0,0 +1,68 @@ +import {TimeoutError} from '../common/Errors.js'; + +/** + * @internal + */ +export interface DeferredPromise<T> extends Promise<T> { + finished: () => boolean; + resolved: () => boolean; + resolve: (_: T) => void; + reject: (_: Error) => void; +} + +/** + * @internal + */ +export interface DeferredPromiseOptions { + message: string; + timeout: number; +} + +/** + * Creates and returns a promise along with the resolve/reject functions. + * + * If the promise has not been resolved/rejected within the `timeout` period, + * the promise gets rejected with a timeout error. `timeout` has to be greater than 0 or + * it is ignored. + * + * @internal + */ +export function createDeferredPromise<T>( + opts?: DeferredPromiseOptions +): DeferredPromise<T> { + let isResolved = false; + let isRejected = false; + let resolver = (_: T): void => {}; + let rejector = (_: Error) => {}; + const taskPromise = new Promise<T>((resolve, reject) => { + resolver = resolve; + rejector = reject; + }); + const timeoutId = + opts && opts.timeout > 0 + ? setTimeout(() => { + isRejected = true; + rejector(new TimeoutError(opts.message)); + }, opts.timeout) + : undefined; + return Object.assign(taskPromise, { + resolved: () => { + return isResolved; + }, + finished: () => { + return isResolved || isRejected; + }, + resolve: (value: T) => { + if (timeoutId) { + clearTimeout(timeoutId); + } + isResolved = true; + resolver(value); + }, + reject: (err: Error) => { + clearTimeout(timeoutId); + isRejected = true; + rejector(err); + }, + }); +} |