Skip to content

Commit 01b6815

Browse files
authored
feat(ts) migrate modules\util\Deferred to TS
1 parent 661fc3b commit 01b6815

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

globals.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export {};
22

3-
declare global {
3+
declare global {
44
type Timeout = ReturnType<typeof setTimeout>;
55
interface Window {
66
connectionTimes: any;

modules/util/Deferred.js renamed to modules/util/Deferred.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
1-
21
/**
32
* Promise-like object which can be passed around for resolving it later. It
43
* implements the "thenable" interface, so it can be used wherever a Promise
54
* could be used.
65
*
76
* In addition a "reject on timeout" functionality is provided.
87
*/
9-
export default class Deferred {
8+
export default class Deferred<T = any> {
9+
promise: Promise<T>;
10+
resolve: (value: T | PromiseLike<T>) => void;
11+
reject: (reason?: any) => void;
12+
then: Promise<T>['then'];
13+
catch: Promise<T>['catch'];
14+
private _timeout?: Timeout;
15+
1016
/**
1117
* Instantiates a Deferred object.
1218
*/
1319
constructor() {
14-
this.promise = new Promise((resolve, reject) => {
15-
this.resolve = (...args) => {
20+
this.promise = new Promise<T>((resolve, reject) => {
21+
this.resolve = (value: T | PromiseLike<T>) => {
1622
this.clearRejectTimeout();
17-
resolve(...args);
23+
resolve(value);
1824
};
19-
this.reject = (...args) => {
25+
this.reject = (reason?: any) => {
2026
this.clearRejectTimeout();
21-
reject(...args);
27+
reject(reason);
2228
};
2329
});
2430
this.then = this.promise.then.bind(this.promise);
@@ -28,14 +34,14 @@ export default class Deferred {
2834
/**
2935
* Clears the reject timeout.
3036
*/
31-
clearRejectTimeout() {
37+
clearRejectTimeout(): void {
3238
clearTimeout(this._timeout);
3339
}
3440

3541
/**
3642
* Rejects the promise after the given timeout.
3743
*/
38-
setRejectTimeout(ms) {
44+
setRejectTimeout(ms: number): void {
3945
this._timeout = setTimeout(() => {
4046
this.reject(new Error('timeout'));
4147
}, ms);

0 commit comments

Comments
 (0)