Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Created August 28, 2018 23:14
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 ThomasG77/ea8c1ed00369f9facd38a643720a49e4 to your computer and use it in GitHub Desktop.
Save ThomasG77/ea8c1ed00369f9facd38a643720a49e4 to your computer and use it in GitHub Desktop.
Promise.race illustrated with sourceforge mirrors
const fetch = require('node-fetch');
const mirrors = ['astuteinternet', 'ayera', 'cfhcable', 'cytranet', 'datapacket', 'excellmedia', 'freefr', 'gigenet', 'iweb', 'jaist', 'kent', 'liquidtelecom', 'managedway', 'nchc', 'netcologne', 'netix', 'newcontinuum', 'phoenixnap', 'pilotfiber', 'razaoinfo', 'superb-dca2', 'superb-sea2', 'svwh', 'tenet', 'ufpr', 'versaweb'];
const url = 'https://sourceforge.net/projects/filezilla/files/FileZilla_Client/3.31.0/FileZilla_3.31.0_win64-setup.exe/download';
// Find the fastest Sourceforge mirror manually
// The usual use case would for Promise.race would be
// when calling images from multiple servers
// (imagine different AWS S3 storage) to get the fastest one
var p = Promise.race(
mirrors.map((mirror, index) => {
return fetch(`${url}?use_mirror=${mirror}`);
})
);
p.then(response => console.log(response.url));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment