Last active
August 29, 2015 14:15
-
-
Save simonmcmanus/553bc763c339501ce203 to your computer and use it in GitHub Desktop.
async-parallel-no-fail
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'; | |
var _ = require('underscore'); | |
/** | |
Custom implementation of Parallel: | |
Async.parallel stops on the first error. | |
This module captures all errors (but does not call the error callback) | |
The result is that if one data request for a page fails, it does not stop | |
the page rendering. | |
The component that failed will just have an error property but the other | |
component will continue to work. | |
* @param {Object} gets { homepage: function(done) { done() }} | |
* @param {Function} callback | |
*/ | |
module.exports = function (gets, callback) { | |
if (_.isEmpty(gets)) { | |
return callback(null, gets); | |
} | |
var counter = 0; | |
var out = {}; | |
var buildOutput = function (property, error, data) { | |
counter++; | |
if (error) { | |
out[property] = { error: error }; | |
}else { | |
out[property] = data; | |
} | |
if (counter === totalGets) { | |
callback(null, out); | |
} | |
}; | |
for (var property in gets) { | |
/* istanbul ignore next */ | |
if (gets.hasOwnProperty(property)) { | |
var totalGets = Object.keys(gets).length; | |
gets[property](buildOutput.bind(null, property)); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment