I am building a web application with node.js (+ angular, etc.).
An application will have to retrieve some data (something like a list of catalog items) from different providers who disclose their data in different ways.
In this module, I will have some function that is common to all providers, and some functions are unique to any of them.
My current (poor) implementation looks something like this:
var providers = [
{ name: 'Apple', },
{ name: 'Samsung', },
{ name: 'Google', },
];
exports.syncCatalogues = function(search, callback) {
var allItems = [];
for (var p = 0; p < providers.length; p++) {
exports.getCatalog(providers[p], function(err, catalog) {
if (err) {
return callback(err);
}
exports.getItems(providers[p], catalog, function(err, items) {
if (err) {
return callback(err);
}
allItems = allItems.concat(items);
callback(null);
});
});
}
};
And my implementation of getCatalog () and getItems () is as ugly as this:
exports.getCatalog(provider, callback) {
if (provider.name === 'Apple') {
}
};
exports.getItems(provider, callback) {
if (provider.name === 'Apple') {
}
};
I know that with ES5 (I adhere) abstract classes are not easy to implement, but I'm sure there is a better way (the code is more readable, maintainable, verifiable) than this ... :-(
source
share