Skip to content

Instantly share code, notes, and snippets.

@fxfactorial
Created July 16, 2017 12:21
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 fxfactorial/828134dc3a98c0f5dbee0a8e76a4de9c to your computer and use it in GitHub Desktop.
Save fxfactorial/828134dc3a98c0f5dbee0a8e76a4de9c to your computer and use it in GitHub Desktop.
'use strict';
const event_emitter = require('events');
class PhotoAnalyzer extends event_emitter {
constructor() {
super();
this.times =
new Array(10).fill(undefined).map(() => Math.floor(Math.random() * 10000));
}
};
const download = new PhotoAnalyzer;
download.on('download-piece-complete', function(part_index) {
console.log(this.times[part_index]);
});
Promise.race(
download.times.map((delay, i) => (
new Promise(accept => setTimeout(() => accept(i), delay))
))
).then(winner => download.emit('download-piece-complete', winner));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment