Skip to content

Instantly share code, notes, and snippets.

@mattraykowski
Created July 28, 2016 18:56
Show Gist options
  • Save mattraykowski/edfdfc6c88660188ad62955ff1c86fe2 to your computer and use it in GitHub Desktop.
Save mattraykowski/edfdfc6c88660188ad62955ff1c86fe2 to your computer and use it in GitHub Desktop.
const _ = require('lodash');
var foo = {
ReportList: {
Report: [
{
Graph: [
{
$: {
title: 'something',
graphtype: 'something'
}
},
{
$: {
title: 'something',
graphtype: 'somethingelse'
}
}
]
},
{
Graph: [
{
$: {
title: 'foobar',
graphtype: 'something'
}
},
{
$: {
title: 'foobarbaz',
graphtype: 'baz'
}
}
]
}
]
}
};
function collectGraphTypes(reportDefinition) {
var allGraphTypes = [];
_.each(foo.ReportList.Report, function(report) {
_.each(report.Graph, function(graph) {
allGraphTypes.push(graph['$'].graphtype);
});
});
allGraphTypes = _.uniq(allGraphTypes);
return allGraphTypes;
}
function getGraphTypeDefinitions(graphTypeList) {
return new Promise(function(resolve, reject) {
var promises = [];
_.each(graphTypeList, function(graphType) {
promises.push(fetchGraphType(graphType));
});
Promise.all(promises).then(
function(value) {
resolve(value);
}
);
})
}
function fetchGraphType(graphType) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve({ newType: graphType });
}, 1000);
});
}
var graphTypes = collectGraphTypes(foo);
getGraphTypeDefinitions(graphTypes).then(
function(allDefs) {
console.log('done!', allDefs);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment