/*! * Copyright (c) Microsoft Corporation and contributors. All rights reserved. * Licensed under the MIT License. */ /** * A deferred creates a promise and the ability to resolve or reject it * * @deprecated Moved to the `@fluidframework/core-utils` package. * @internal */ export class Deferred { private readonly p: Promise; private res: ((value: T | PromiseLike) => void) | undefined; private rej: ((reason?: any) => void) | undefined; private completed: boolean = false; constructor() { this.p = new Promise((resolve, reject) => { this.res = resolve; this.rej = reject; }); } /** * Returns whether the underlying promise has been completed */ public get isCompleted(): boolean { return this.completed; } /** * Retrieves the underlying promise for the deferred * * @returns the underlying promise */ public get promise(): Promise { return this.p; } /** * Resolves the promise * * @param value - the value to resolve the promise with */ public resolve(value: T | PromiseLike): void { if (this.res !== undefined) { this.completed = true; this.res(value); } } /** * Rejects the promise * * @param value - the value to reject the promise with */ public reject(error: any): void { if (this.rej !== undefined) { this.completed = true; this.rej(error); } } } /** * A lazy evaluated promise. The execute function is delayed until * the promise is used, e.g. await, then, catch ... * The execute function is only called once. * All calls are then proxied to the promise returned by the execute method. * * @deprecated Moved to the `@fluidframework/core-utils` package. * @internal */ export class LazyPromise implements Promise { public get [Symbol.toStringTag](): string { return this.getPromise()[Symbol.toStringTag]; } private result: Promise | undefined; constructor(private readonly execute: () => Promise) {} public async then( // eslint-disable-next-line @rushstack/no-new-null onfulfilled?: ((value: T) => TResult1 | PromiseLike) | null | undefined, // eslint-disable-next-line @rushstack/no-new-null onrejected?: ((reason: any) => TResult2 | PromiseLike) | null | undefined, ): Promise { // eslint-disable-next-line prefer-rest-params return this.getPromise().then(...arguments); } public async catch( // eslint-disable-next-line @rushstack/no-new-null onrejected?: ((reason: any) => TResult | PromiseLike) | null | undefined, ): Promise { // eslint-disable-next-line prefer-rest-params return this.getPromise().catch(...arguments); } // eslint-disable-next-line @rushstack/no-new-null public async finally(onfinally?: (() => void) | null | undefined): Promise { // eslint-disable-next-line prefer-rest-params return this.getPromise().finally(...arguments); } private async getPromise(): Promise { if (this.result === undefined) { this.result = this.execute(); } return this.result; } }