Presented below is a complex function that aims to extract model variants from a csv file, which are stored as strings in an array. The path of this csv file depends on information obtained from other csv files, hence the need for looping.
The csvService.getCsvAsArray
call retrieves the contents of a csv file as an object, with each column stored as an array under an attribute named after the top column. When you encounter expressions like
result["NavigationSectionShortNames"]
, remember that it represents an array of strings.
var INDEX_OF_PRODUCTS_SECTION = 1;
var getAllModelVariants = function () {
return csvService.getCsvAsArray("Contents/Sections/" + navFileNames[INDEX_OF_PRODUCTS_SECTION] + "/order.csv").then(function (result) {
var products = [];
for (var i = 0; i < result["NavigationSectionShortNames"].length; i++) {
csvService.getCsvAsArray("Contents/Sections/" + navFileNames[INDEX_OF_PRODUCTS_SECTION] + "/" + result["NavigationSectionShortNames"][i]
+ "/order.csv").then(function (productModelInfo) {
var productFamilies = [];
for (var j = 0; j < productModelInfo["NavigationSectionShortNames"].length; j++) {
csvService.getCsvAsArray("Contents/Sections/" + navFileNames[INDEX_OF_PRODUCTS_SECTION].length + "/" + result["NavigationSectionShortNames"][i] + "/" + productModelInfo["NavigationSectionShortNames"][j] + "/order.csv").then(function (modelVariants) {
var productModels = [];
for (var k = 0; k < modelVariants.length; k++) {
productModels.push(modelVariants["NavigationSections"][k]);
};
productFamilies.push(productModels);
});
};
products.push(productFamilies);
});
};
})
return products;
};
This current approach may not work due to the asynchronous nature of promises causing variable increments to change before resolution. Is there a solution for using promises effectively in nested loops like these? I have considered using $q.all
but applying it to my function is challenging. As I explore options, I found an example on using promises in a single loop at this link.
My objective is to return a 3D array structure, exemplified in the plunker linked below for reference.
If needed, here's a simpler version of the function without promises:
var getAllModelVariantsTest = function () {
var result = ["productFamily1", "productFamily2", "productFamily3"];
var testArray = ["productFamilyModel1", "productFamilyModelt2", "productFamilyModel3", "productFamilyModel4"];
var testArray3 = ["productFamilyModelVariant1", "productFamilyModelVariant2", "productFamilyModelVariant3", "productFamilyModelVariant4"];
var products = [];
for (var i = 0; i < result.length; i++) {
var productFamilies = [];
for (var j = 0; j < testArray.length; j++) {
var productModels = [];
for (var k = 0; k < testArray3.length; k++) {
productModels.push(testArray3[k]);
};
productFamilies.push(productModels);
};
products.push(productFamilies);
};
return products;
};
var testOutput = getAllModelVariantsTest();
You can view the desired output by running the above code snippet or accessing the plunker demo through this link.
I seek assistance in implementing promises within nested loops successfully to achieve results akin to the simplified function version. Is utilizing $q.all
the recommended course of action?
Your insights and expertise are highly appreciated. Feel free to request additional details if needed.