Created
July 28, 2016 18:56
-
-
Save mattraykowski/edfdfc6c88660188ad62955ff1c86fe2 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
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