Skip to content

Instantly share code, notes, and snippets.

@lavelle
Created October 1, 2018 21:46
Show Gist options
  • Save lavelle/957669db1fefdfc194cfe257c4885b85 to your computer and use it in GitHub Desktop.
Save lavelle/957669db1fefdfc194cfe257c4885b85 to your computer and use it in GitHub Desktop.
function fruitFetcher(prefix, callback) {
callback([
'apples',
'bananas',
'oranges',
]);
}
function animalFetcher(prefix, callback) {
callback([
'cats',
'dogs',
'sheep',
]);
}
function combineFetchers(fetchers) {
return function(prefix, callback) {
let allResults = [];
let numFinished = 0;
fetchers.forEach(fetcher => {
fetcher(prefix, (results) => {
allResults = allResults.concat(results);
numFinished++;
if (numFinished === fetchers.length) {
callback(allResults);
}
});
});
}
}
const allFetcher = combineFetchers([fruitFetcher, animalFetcher]);
allFetcher('aaa', (results) => {
console.log(results);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment