Skip to content

Commit cc115e8

Browse files
author
Benjamin Pasero
committed
debt - more async/await adoption
1 parent cd8000e commit cc115e8

17 files changed

Lines changed: 298 additions & 287 deletions

File tree

src/vs/base/common/actions.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,18 +191,19 @@ export class ActionRunner extends Disposable implements IActionRunner {
191191
private _onDidRun = this._register(new Emitter<IRunEvent>());
192192
readonly onDidRun: Event<IRunEvent> = this._onDidRun.event;
193193

194-
run(action: IAction, context?: any): Promise<any> {
194+
async run(action: IAction, context?: any): Promise<any> {
195195
if (!action.enabled) {
196196
return Promise.resolve(null);
197197
}
198198

199199
this._onDidBeforeRun.fire({ action: action });
200200

201-
return this.runAction(action, context).then((result: any) => {
201+
try {
202+
const result = await this.runAction(action, context);
202203
this._onDidRun.fire({ action: action, result: result });
203-
}, (error: any) => {
204+
} catch (error) {
204205
this._onDidRun.fire({ action: action, error: error });
205-
});
206+
}
206207
}
207208

208209
protected runAction(action: IAction, context?: any): Promise<any> {

src/vs/base/node/storage.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -554,10 +554,12 @@ export class SQLiteStorageDatabase implements IStorageDatabase {
554554
return integrity;
555555
}
556556

557-
private connect(path: string, retryOnBusy: boolean = true): Promise<IDatabaseConnection> {
557+
private async connect(path: string, retryOnBusy: boolean = true): Promise<IDatabaseConnection> {
558558
this.logger.trace(`[storage ${this.name}] open(${path}, retryOnBusy: ${retryOnBusy})`);
559559

560-
return this.doConnect(path).then(undefined, error => {
560+
try {
561+
return await this.doConnect(path);
562+
} catch (error) {
561563
this.logger.error(`[storage ${this.name}] open(): Unable to open DB due to ${error}`);
562564

563565
// SQLITE_BUSY should only arise if another process is locking the same DB we want
@@ -569,7 +571,9 @@ export class SQLiteStorageDatabase implements IStorageDatabase {
569571
// In this case we simply wait for some time and retry once to establish the connection.
570572
//
571573
if (error.code === 'SQLITE_BUSY' && retryOnBusy) {
572-
return timeout(SQLiteStorageDatabase.BUSY_OPEN_TIMEOUT).then(() => this.connect(path, false /* not another retry */));
574+
await timeout(SQLiteStorageDatabase.BUSY_OPEN_TIMEOUT);
575+
576+
return this.connect(path, false /* not another retry */);
573577
}
574578

575579
// Otherwise, best we can do is to recover from a backup if that exists, as such we
@@ -579,17 +583,19 @@ export class SQLiteStorageDatabase implements IStorageDatabase {
579583
// The final fallback is to use an in-memory DB which should only happen if the target
580584
// folder is really not writeable for us.
581585
//
582-
return unlink(path)
583-
.then(() => renameIgnoreError(this.toBackupPath(path), path))
584-
.then(() => this.doConnect(path))
585-
.then(undefined, error => {
586-
this.logger.error(`[storage ${this.name}] open(): Unable to use backup due to ${error}`);
587-
588-
// In case of any error to open the DB, use an in-memory
589-
// DB so that we always have a valid DB to talk to.
590-
return this.doConnect(SQLiteStorageDatabase.IN_MEMORY_PATH);
591-
});
592-
});
586+
try {
587+
await unlink(path);
588+
await renameIgnoreError(this.toBackupPath(path), path);
589+
590+
return await this.doConnect(path);
591+
} catch (error) {
592+
this.logger.error(`[storage ${this.name}] open(): Unable to use backup due to ${error}`);
593+
594+
// In case of any error to open the DB, use an in-memory
595+
// DB so that we always have a valid DB to talk to.
596+
return this.doConnect(SQLiteStorageDatabase.IN_MEMORY_PATH);
597+
}
598+
}
593599
}
594600

595601
private handleSQLiteError(connection: IDatabaseConnection, error: Error & { code?: string }, msg: string): void {

src/vs/base/test/node/stream/stream.test.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,19 @@ import * as stream from 'vs/base/node/stream';
99
import { getPathFromAmdModule } from 'vs/base/common/amd';
1010

1111
suite('Stream', () => {
12-
test('readToMatchingString - ANSI', function () {
12+
test('readToMatchingString - ANSI', async () => {
1313
const file = getPathFromAmdModule(require, './fixtures/file.css');
1414

15-
return stream.readToMatchingString(file, '\n', 10, 100).then((result: string) => {
16-
// \r may be present on Windows
17-
assert.equal(result.replace('\r', ''), '/*---------------------------------------------------------------------------------------------');
18-
});
15+
const result = await stream.readToMatchingString(file, '\n', 10, 100);
16+
17+
// \r may be present on Windows
18+
assert.equal(result!.replace('\r', ''), '/*---------------------------------------------------------------------------------------------');
1919
});
2020

21-
test('readToMatchingString - empty', function () {
21+
test('readToMatchingString - empty', async () => {
2222
const file = getPathFromAmdModule(require, './fixtures/empty.txt');
2323

24-
return stream.readToMatchingString(file, '\n', 10, 100).then((result: string) => {
25-
assert.equal(result, null);
26-
});
24+
const result = await stream.readToMatchingString(file, '\n', 10, 100);
25+
assert.equal(result, null);
2726
});
2827
});

src/vs/platform/lifecycle/common/lifecycleService.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ export abstract class AbstractLifecycleService extends Disposable implements ILi
5858
}
5959
}
6060

61-
when(phase: LifecyclePhase): Promise<void> {
61+
async when(phase: LifecyclePhase): Promise<void> {
6262
if (phase <= this._phase) {
63-
return Promise.resolve();
63+
return;
6464
}
6565

6666
let barrier = this.phaseWhen.get(phase);
@@ -69,6 +69,6 @@ export abstract class AbstractLifecycleService extends Disposable implements ILi
6969
this.phaseWhen.set(phase, barrier);
7070
}
7171

72-
return barrier.wait().then(undefined);
72+
await barrier.wait();
7373
}
7474
}

src/vs/platform/lifecycle/electron-browser/lifecycleService.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,17 @@ export class LifecycleService extends AbstractLifecycleService {
7575
});
7676

7777
// Main side indicates that we will indeed shutdown
78-
ipc.on('vscode:onWillUnload', (_event: unknown, reply: { replyChannel: string, reason: ShutdownReason }) => {
78+
ipc.on('vscode:onWillUnload', async (_event: unknown, reply: { replyChannel: string, reason: ShutdownReason }) => {
7979
this.logService.trace(`lifecycle: onWillUnload (reason: ${reply.reason})`);
8080

8181
// trigger onWillShutdown events and joining
82-
return this.handleWillShutdown(reply.reason).then(() => {
82+
await this.handleWillShutdown(reply.reason);
8383

84-
// trigger onShutdown event now that we know we will quit
85-
this._onShutdown.fire();
84+
// trigger onShutdown event now that we know we will quit
85+
this._onShutdown.fire();
8686

87-
// acknowledge to main side
88-
ipc.send(reply.replyChannel, windowId);
89-
});
87+
// acknowledge to main side
88+
ipc.send(reply.replyChannel, windowId);
9089
});
9190

9291
// Save shutdown reason to retrieve on next startup
@@ -111,7 +110,7 @@ export class LifecycleService extends AbstractLifecycleService {
111110
});
112111
}
113112

114-
private handleWillShutdown(reason: ShutdownReason): Promise<void> {
113+
private async handleWillShutdown(reason: ShutdownReason): Promise<void> {
115114
const joiners: Promise<void>[] = [];
116115

117116
this._onWillShutdown.fire({
@@ -123,9 +122,11 @@ export class LifecycleService extends AbstractLifecycleService {
123122
reason
124123
});
125124

126-
return Promise.all(joiners).then(() => undefined, err => {
127-
this.notificationService.error(toErrorMessage(err));
128-
onUnexpectedError(err);
129-
});
125+
try {
126+
await Promise.all(joiners);
127+
} catch (error) {
128+
this.notificationService.error(toErrorMessage(error));
129+
onUnexpectedError(error);
130+
}
130131
}
131132
}

src/vs/platform/lifecycle/electron-main/lifecycleMain.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ export class LifecycleService extends Disposable implements ILifecycleService {
291291
});
292292
}
293293

294-
unload(window: ICodeWindow, reason: UnloadReason): Promise<boolean /* veto */> {
294+
async unload(window: ICodeWindow, reason: UnloadReason): Promise<boolean /* veto */> {
295295

296296
// Always allow to unload a window that is not yet ready
297297
if (!window.isReady) {
@@ -302,27 +302,27 @@ export class LifecycleService extends Disposable implements ILifecycleService {
302302

303303
// first ask the window itself if it vetos the unload
304304
const windowUnloadReason = this._quitRequested ? UnloadReason.QUIT : reason;
305-
return this.onBeforeUnloadWindowInRenderer(window, windowUnloadReason).then(veto => {
306-
if (veto) {
307-
this.logService.trace(`Lifecycle#unload() - veto in renderer (window ID ${window.id})`);
305+
let veto = await this.onBeforeUnloadWindowInRenderer(window, windowUnloadReason);
306+
if (veto) {
307+
this.logService.trace(`Lifecycle#unload() - veto in renderer (window ID ${window.id})`);
308308

309-
return this.handleWindowUnloadVeto(veto);
310-
}
309+
return this.handleWindowUnloadVeto(veto);
310+
}
311311

312-
// then check for vetos in the main side
313-
return this.onBeforeUnloadWindowInMain(window, windowUnloadReason).then(veto => {
314-
if (veto) {
315-
this.logService.trace(`Lifecycle#unload() - veto in main (window ID ${window.id})`);
312+
// then check for vetos in the main side
313+
veto = await this.onBeforeUnloadWindowInMain(window, windowUnloadReason);
314+
if (veto) {
315+
this.logService.trace(`Lifecycle#unload() - veto in main (window ID ${window.id})`);
316316

317-
return this.handleWindowUnloadVeto(veto);
318-
}
317+
return this.handleWindowUnloadVeto(veto);
318+
}
319319

320-
this.logService.trace(`Lifecycle#unload() - no veto (window ID ${window.id})`);
320+
this.logService.trace(`Lifecycle#unload() - no veto (window ID ${window.id})`);
321321

322-
// finally if there are no vetos, unload the renderer
323-
return this.onWillUnloadWindowInRenderer(window, windowUnloadReason).then(() => false);
324-
});
325-
});
322+
// finally if there are no vetos, unload the renderer
323+
await this.onWillUnloadWindowInRenderer(window, windowUnloadReason);
324+
325+
return false;
326326
}
327327

328328
private handleWindowUnloadVeto(veto: boolean): boolean {

src/vs/platform/state/node/stateService.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,23 @@ export class FileStorage {
2626
return this._database;
2727
}
2828

29-
init(): Promise<void> {
30-
return readFile(this.dbPath).then(contents => {
29+
async init(): Promise<void> {
30+
try {
31+
const contents = await readFile(this.dbPath);
32+
3133
try {
3234
this.lastFlushedSerializedDatabase = contents.toString();
3335
this._database = JSON.parse(this.lastFlushedSerializedDatabase);
3436
} catch (error) {
3537
this._database = {};
3638
}
37-
}, error => {
39+
} catch (error) {
3840
if (error.code !== 'ENOENT') {
3941
this.onError(error);
4042
}
4143

4244
this._database = {};
43-
});
45+
}
4446
}
4547

4648
private loadSync(): object {

src/vs/platform/state/test/node/state.test.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,37 @@ suite('StateService', () => {
1414
const parentDir = getRandomTestPath(os.tmpdir(), 'vsctests', 'stateservice');
1515
const storageFile = path.join(parentDir, 'storage.json');
1616

17-
teardown(done => {
18-
rimraf(parentDir, RimRafMode.MOVE).then(done, done);
17+
teardown(async () => {
18+
await rimraf(parentDir, RimRafMode.MOVE);
1919
});
2020

21-
test('Basics', () => {
22-
return mkdirp(parentDir).then(() => {
23-
writeFileSync(storageFile, '');
21+
test('Basics', async () => {
22+
await mkdirp(parentDir);
23+
writeFileSync(storageFile, '');
2424

25-
let service = new FileStorage(storageFile, () => null);
25+
let service = new FileStorage(storageFile, () => null);
2626

27-
service.setItem('some.key', 'some.value');
28-
assert.equal(service.getItem('some.key'), 'some.value');
27+
service.setItem('some.key', 'some.value');
28+
assert.equal(service.getItem('some.key'), 'some.value');
2929

30-
service.removeItem('some.key');
31-
assert.equal(service.getItem('some.key', 'some.default'), 'some.default');
30+
service.removeItem('some.key');
31+
assert.equal(service.getItem('some.key', 'some.default'), 'some.default');
3232

33-
assert.ok(!service.getItem('some.unknonw.key'));
33+
assert.ok(!service.getItem('some.unknonw.key'));
3434

35-
service.setItem('some.other.key', 'some.other.value');
35+
service.setItem('some.other.key', 'some.other.value');
3636

37-
service = new FileStorage(storageFile, () => null);
37+
service = new FileStorage(storageFile, () => null);
3838

39-
assert.equal(service.getItem('some.other.key'), 'some.other.value');
39+
assert.equal(service.getItem('some.other.key'), 'some.other.value');
4040

41-
service.setItem('some.other.key', 'some.other.value');
42-
assert.equal(service.getItem('some.other.key'), 'some.other.value');
41+
service.setItem('some.other.key', 'some.other.value');
42+
assert.equal(service.getItem('some.other.key'), 'some.other.value');
4343

44-
service.setItem('some.undefined.key', undefined);
45-
assert.equal(service.getItem('some.undefined.key', 'some.default'), 'some.default');
44+
service.setItem('some.undefined.key', undefined);
45+
assert.equal(service.getItem('some.undefined.key', 'some.default'), 'some.default');
4646

47-
service.setItem('some.null.key', null);
48-
assert.equal(service.getItem('some.null.key', 'some.default'), 'some.default');
49-
});
47+
service.setItem('some.null.key', null);
48+
assert.equal(service.getItem('some.null.key', 'some.default'), 'some.default');
5049
});
5150
});

0 commit comments

Comments
 (0)