Skip to content

Instantly share code, notes, and snippets.

@simonmcmanus
Last active August 29, 2015 14:15
Show Gist options
  • Save simonmcmanus/553bc763c339501ce203 to your computer and use it in GitHub Desktop.
Save simonmcmanus/553bc763c339501ce203 to your computer and use it in GitHub Desktop.
async-parallel-no-fail
'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