Created
March 21, 2016 17:19
-
-
Save raisch/2751f605f75cf9383748 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
/* Created by raisch on 3/21/16. */ | |
/*jshint node:true, bitwise:true, camelcase:false, curly:true, undef:false, unused:false, eqeqeq:true, shadow:true */ | |
'use strict'; | |
var assert = require('assert'), | |
event = require('events'); | |
/** | |
* MOCK for penthouse | |
* @param {object} opts | |
* @property {string} opts.url | |
* @param {function} cb | |
*/ | |
var penthouse = function(opts, cb) { | |
console.log('url: %s', opts.url); | |
cb(null, 'RESULT FOR ' + opts.url); | |
}; | |
var emitter = new event.EventEmitter(); | |
/** MOCK - Container for URL records read from CSV file. | |
* | |
* @type {Array} | |
*/ | |
var urls = [ | |
[null, null, 'url1'], | |
[null, null, 'url2'], | |
[null, null, 'url3'] | |
]; | |
/** Event handler to process a single URL | |
* | |
* @emits processUrl | |
*/ | |
var onProcessUrl = function() { | |
// always check your assumptions | |
assert(Array.isArray(urls), 'urls must be an array'); | |
var urlRecord = urls.shift(); | |
if(urlRecord) { | |
assert(Array.isArray(urlRecord), 'urlRecord must be an array'); | |
assert(urlRecord.length > 2, 'urlRecord must have at least three elements'); | |
penthouse( | |
{ | |
url: urlRecord[2] | |
}, | |
function(e, res) { | |
if(e) { | |
console.error('failed to process record %s: %s', urlRecord, e); | |
return; // IMPORTANT! do not drop through to rest of func! | |
} | |
// do what you need with the result here | |
console.log('RESULT: %s', res) | |
if(urls.length === 0) { // ok, we're done | |
console.log('completed processing URLs'); | |
return; | |
} | |
emitter.emit('processUrl'); | |
} | |
); | |
} | |
} | |
/** | |
* processUrl event - triggers processing of next URL | |
* | |
* @event processUrl | |
*/ | |
emitter.on('processUrl', onProcessUrl); // assign handler | |
// start... | |
emitter.emit('processUrl'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment