diff --git a/packages/taler-wallet-core/src/util/promiseUtils.ts b/packages/taler-wallet-core/src/util/promiseUtils.ts index d409686d9..23f1c06a5 100644 --- a/packages/taler-wallet-core/src/util/promiseUtils.ts +++ b/packages/taler-wallet-core/src/util/promiseUtils.ts @@ -23,6 +23,8 @@ export interface OpenedPromise { /** * Get an unresolved promise together with its extracted resolve / reject * function. + * + * Recent ECMAScript proposals also call this a promise capability. */ export function openPromise(): OpenedPromise { let resolve: ((x?: any) => void) | null = null; @@ -39,22 +41,20 @@ export function openPromise(): OpenedPromise { } export class AsyncCondition { - private _waitPromise: Promise; - private _resolveWaitPromise: (val: void) => void; - constructor() { - const op = openPromise(); - this._waitPromise = op.promise; - this._resolveWaitPromise = op.resolve; - } + private promCap?: OpenedPromise = undefined; + constructor() {} wait(): Promise { - return this._waitPromise; + if (!this.promCap) { + this.promCap = openPromise(); + } + return this.promCap.promise; } trigger(): void { - this._resolveWaitPromise(); - const op = openPromise(); - this._waitPromise = op.promise; - this._resolveWaitPromise = op.resolve; + if (this.promCap) { + this.promCap.resolve(); + } + this.promCap = undefined; } }