Skip to content

Instantly share code, notes, and snippets.

@buesing
Created June 28, 2019 08:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buesing/884fa5619d07b75decdc755966e3e4a2 to your computer and use it in GitHub Desktop.
Save buesing/884fa5619d07b75decdc755966e3e4a2 to your computer and use it in GitHub Desktop.
chooseClosestServer() {
// try connecting to every available server, choose the one that answers first
return new Promise((resolve, reject) => {
Promise.race(this.availableServers.map((server, index) => this.pingServer(index))).then(fastestServer => {
if (fastestServer === 'timeout') {
reject(fastestServer);
return;
}
this.chosenServer = fastestServer;
// eslint-disable-next-line
return this.connectToServer(this.availableServers[fastestServer]);
}).then(() => {
resolve();
}).catch(e => {
console.warn(`error: ${e}`);
reject(e);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment