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