Path: blob/main/components/gitpod-protocol/src/util/deferred.ts
2500 views
/**1* Copyright (c) 2020 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56export class Deferred<T> {7resolve: (value?: T) => void;8reject: (err?: any) => void;9isResolved: boolean = false;10timer: NodeJS.Timeout;1112constructor(timeout?: number) {13if (timeout) {14this.timer = setTimeout(() => this.reject(new Error(`Timeout of ${timeout} ms.`)), timeout);15}16}1718promise = new Promise<T>((resolve, reject) => {19this.resolve = (o?: T) => {20this.isResolved = true;21resolve(o as T);22clearTimeout(this.timer);23};24this.reject = (e) => {25reject(e);26clearTimeout(this.timer);27};28});29}303132