0

I am trying to use the tableau JS API https://help.tableau.com/current/api/js_api/en-us/JavaScriptAPI/js_api_ref.htm to gather all the data from every "worksheet" object to do some more complex behavior but I am having trouble understanding how to use the promises within loops. I've used when().then() with ajax before, but not much other experience with promises.

Worksheet -> Marks -> Pairs -> key/value
  • I start with a sheets object variable that is instantiated with "worksheets".
  • I loop through each "worksheet" to get the "marks" which has data key/value "pairs".
  • I save every data pair into another object variable markpairs_object.
  • The part I have issues with is figuring out how to use the final object variable markpairs_object because it's always empty at execution time.
sheets[window][section] = tableau "worksheets".

markpairs_object = [];
for(var section in sheets){
    for(var window in section){
        // ASYNC getting marks
        sheets[section][window].getSelectedMarksAsync().then(function(marks){
            for(var markIndex=0; markIndex<marks.length; markIndex++){
                var pairs = marks[markIndex].getPairs(); // I don't think this is ASYNC
                for(var pairIndex=0; pairIndex<pairs.length; pairIndex++){
                    var pair = pairs[pairIndex];
                    // save pair info to an overall object variable
                    markpairs_object[pair.fieldName] = pair.formattedValue;
                }
            }
        });
    }
}

//loop through object variable to make sure it worked
whatever I do here shows empty

I've tried:

  • using callbacks
  • moving different things into their own functions but didn't understand how to implement await/async here
  • another .then() after the getSelectedMarksAsync().then() but that would only get the pairs for one worksheet at a time if it worked (it didn't)
  • creating flag variables to try to indicate when pairs data has been saved

etc. but the object is always empty at the time of execution and I could not understand how to get this to work correctly.

Zero
  • 101
  • 2

0 Answers0