adding timeout to awaitAvailability
This commit is contained in:
parent
2fadcfa407
commit
1d562f6997
1 changed files with 7 additions and 2 deletions
|
@ -308,10 +308,11 @@ export class Guild extends Base {
|
|||
|
||||
/**
|
||||
* Fulfills promise when guild becomes available
|
||||
* @param timeout Configurable timeout to cancel the wait to safely remove listener.
|
||||
*/
|
||||
async awaitAvailability(): Promise<Guild> {
|
||||
async awaitAvailability(timeout: number = 1000): Promise<Guild> {
|
||||
return await new Promise((resolve, reject) => {
|
||||
if(!this.unavailable) return;
|
||||
if(!this.unavailable) resolve(this);
|
||||
const listener = (guild: Guild): void => {
|
||||
if (guild.id === this.id) {
|
||||
this.client.removeListener('guildLoaded', listener);
|
||||
|
@ -319,6 +320,10 @@ export class Guild extends Base {
|
|||
}
|
||||
};
|
||||
this.client.on('guildLoaded', listener);
|
||||
setTimeout(() => {
|
||||
this.client.removeListener('guildLoaded', listener);
|
||||
reject(Error("Timeout. Guild didn't arrive in time."));
|
||||
}, timeout);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue