forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync.ts
More file actions
66 lines (61 loc) · 1.91 KB
/
async.ts
File metadata and controls
66 lines (61 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
export async function sleep(timeout: number) {
return new Promise((resolve) => {
setTimeout(resolve, timeout);
});
}
//======================
// Deferred
// tslint:disable-next-line:interface-name
export interface Deferred<T> {
readonly promise: Promise<T>;
readonly resolved: boolean;
readonly rejected: boolean;
readonly completed: boolean;
resolve(value?: T | PromiseLike<T>);
// tslint:disable-next-line:no-any
reject(reason?: any);
}
class DeferredImpl<T> implements Deferred<T> {
private _resolve!: (value?: T | PromiseLike<T>) => void;
// tslint:disable-next-line:no-any
private _reject!: (reason?: any) => void;
private _resolved: boolean = false;
private _rejected: boolean = false;
private _promise: Promise<T>;
// tslint:disable-next-line:no-any
constructor(private scope: any = null) {
// tslint:disable-next-line:promise-must-complete
this._promise = new Promise<T>((res, rej) => {
this._resolve = res;
this._reject = rej;
});
}
public resolve(value?: T | PromiseLike<T>) {
this._resolve.apply(this.scope ? this.scope : this, arguments);
this._resolved = true;
}
// tslint:disable-next-line:no-any
public reject(reason?: any) {
this._reject.apply(this.scope ? this.scope : this, arguments);
this._rejected = true;
}
get promise(): Promise<T> {
return this._promise;
}
get resolved(): boolean {
return this._resolved;
}
get rejected(): boolean {
return this._rejected;
}
get completed(): boolean {
return this._rejected || this._resolved;
}
}
// tslint:disable-next-line:no-any
export function createDeferred<T>(scope: any = null): Deferred<T> {
return new DeferredImpl<T>(scope);
}