Simplifying promises
This commit is contained in:
parent
3cfb74be54
commit
1bb6d893e0
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user