
I'm making a worker that looks through several repeating sections, grabs a certain value from each of them, and adds them all together. This all hinges on running through a loop that calls getSectionIDs() for each relevant section and, because it's asynchronous, the variable I'm storing the results in is empty when I try to use it. I have it set up with a promise right now (which I honestly barely understand), but it still gives the same result. // 'modSections' contains a list of names for sections that are to be used in this function. // 'modNames' contains a list of attribute names formatted for the user // 'attrNames' contains a list of the actual attribute names used on the sheet (matches up with 'modNames') modSections.forEach(function(value) { // Create listener for each section with modifiers on(`change:repeating_${value}:${value}mods`, function() { // Build query for getAttrs() for each section with modifiers var makeQuery = new Promise(function(resolve) { var query = []; modSections.forEach(function(section) // For each repeating section... { getSectionIDs(section, function(ids) // ...get a list of IDs in that section... { ids.forEach(function(id) // ...and for each ID... { // ...add it to the query query.push(`repeating_${section}_${id}_${section}mods`); }); }); }); resolve(query); }); var setMods = function() { makeQuery.then(function(query) { getAttrs(query, function(modInputs) // Get all modifier strings from all repeating sections { var modString = ""; query.forEach(function(q) // Combine all modifier strings into one string { modString.concat(modInputs[q]); modString.concat(","); // Comma separator }); mods = modString.replace(" ", "").toLowerCase().split(",").filter(Boolean); // Cleaning up and splitting into individual modifier strings var modTotals = new Array(modNames.length); modTotals.fill(0); mods.forEach(function(mod) { let props = mod.split(":"); // Separate each modifier into its properties modTotals[modNames.indexOf(props[0])] += parseInt(props[1]); // Total up all properties for each attribute name }); for(i = 0; i < modTotals.length; i++) { let attrName = attrNames[i]; let modTotal = modTotals[i]; setAttrs({ attrName : modTotal }); } }); }); } setMods(); }); });