Simplifying promises

This commit is contained in:
David Négrier 2021-07-21 18:21:12 +02:00
parent 3cfb74be54
commit 1bb6d893e0
2 changed files with 27 additions and 42 deletions

View File

@ -436,18 +436,10 @@ export class GameRoom {
private getVariableManager(): Promise<VariablesManager> {
if (!this.variableManagerPromise) {
this.variableManagerPromise = new Promise<VariablesManager>((resolve, reject) => {
this.getMap()
this.variableManagerPromise = this.getMap()
.then((map) => {
const variablesManager = new VariablesManager(this.roomUrl, map);
variablesManager
.init()
.then(() => {
resolve(variablesManager);
})
.catch((e) => {
reject(e);
});
return variablesManager.init();
})
.catch((e) => {
if (e instanceof LocalUrlError) {
@ -465,19 +457,11 @@ export class GameRoom {
}, 1000);
const variablesManager = new VariablesManager(this.roomUrl, null);
variablesManager
.init()
.then(() => {
resolve(variablesManager);
})
.catch((e) => {
reject(e);
});
return variablesManager.init();
} else {
reject(e);
throw e;
}
});
});
}
return this.variableManagerPromise;
}

View File

@ -45,14 +45,15 @@ export class VariablesManager {
/**
* Let's load data from the Redis backend.
*/
public async init(): Promise<void> {
public async init(): Promise<VariablesManager> {
if (!this.shouldPersist()) {
return;
return this;
}
const variables = await variablesRepository.loadVariables(this.roomUrl);
for (const key in variables) {
this._variables.set(key, variables[key]);
}
return this;
}
/**