Created
July 16, 2017 12:21
-
-
Save fxfactorial/828134dc3a98c0f5dbee0a8e76a4de9c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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