/** * Copyright (c) 2020 TypeFox GmbH. All rights reserved. * Licensed under the GNU Affero General Public License (AGPL). * See License-AGPL.txt in the project root for license information. */ export class Deferred { resolve: (value?: T) => void; reject: (err?: any) => void; isResolved: boolean = false; timer: NodeJS.Timer; constructor(timeout?: number) { if (timeout) { this.timer = setTimeout(() => this.reject(new Error(`Timeout of ${timeout} ms.`)), timeout); } } promise = new Promise((resolve, reject) => { this.resolve = (o) => { this.isResolved = true; resolve(o) clearTimeout(this.timer) }; this.reject = (e) => { reject(e) clearTimeout(this.timer) } }); }