Created
August 8, 2018 12:26
-
-
Save sleepyfox/297c35af2ec8b47bc6b0351dd4cf048d 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
// An approach based upon event emitters | |
const EventEmitter = require('events') | |
const getLargeExternalDataEvent = (url) => { | |
setTimeout(() => { | |
console.log('fetching data from ' + url + '...') | |
q.emit('data', genLongString(url)) | |
} , 1000) | |
} | |
const getSizeEvent = (data) => { | |
setTimeout(() => { | |
console.log('calculating size of data...') | |
q.emit('size', data.length) | |
}, 1000) | |
} | |
const intoMBsEvent = (size) => { | |
setTimeout(() => { | |
q.emit('mbs', (size / 1024)) | |
}, 1000) | |
} | |
const q = new EventEmitter() | |
q.on('data', (data) => getSizeEvent(data)) | |
q.on('size', (size) => intoMBsEvent(size)) | |
q.on('mbs', (mbs) => postResult(mbs)) | |
getLargeExternalDataEvent('wat.event') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment