Tableau JS API provides multitude of options for interacting, fetching and reusing data from your viz. Mostly, for performance reasons, part of the calls are made in async manner.
Asynchronous part of the Tableau JS API is delivered using Promises methodology. For experienced JavaScript developer is not a big deal, but for the rookies can be a mind-bender. Let’s see have we can handle Promises response with Tableau JavaScript and take benefit from async calls.

According to Tableau JS API documentation Promise returns an Array so it can be easily iterated.

Use Case

Let’s consider simple example. I have 2 filters on dashboard: Order Data and Country. I am interested in getting current values of those filters and displaying it below the graph. Easiest way to do this is use Tableau JavaScript function – getFiltersAsync() which will provide list of filters so I can fetch their values. However, running simple code like this:

will most likely return 1 random filter to the filtersArray and proceed with next code.. Now the issue is clear – Tableau Javascript code proceeds with next operations without waiting for the asynchronous operation to finish

Solution

To make sure that script waits for all filters to be listed, we need to wrap filtersearch() function in .then() callback provided by Tableau API. Code inside .then() callback is performed when operations in the previous function complete.

Note that filtername in the function wrapped in .then() holds the object returned from the filterseach() function. This way we can still benefit from the data returned from the first function but after all async operations are finished.

Unfortunately, I don’t think it’s possible.. I think you can get only applied values. Also you can fetch field name used for fitlering.. Maybe then iterating using the selectMarksAsync() you may get the desired results.. but it’s something that needs to be investigated..

Vote Up0Vote Down Reply

1 year ago

Guest

joe shabadoo

Hi, Can you explain this line:if (field.search("AutoFilter")>0 && field.search("Action")==-1 )

I am not familiar with .search and am not sure what “AutoFilter” and “Action” refer to.
Are these strings representing the name of the filter?

.search is a JS native function. Basically what we would like to do here is to find in field array filters that have name AutoFilter. So yes – this is the name of the filter and it’s case sensitive. Since this post I’ve improved my knowledge on promises in general and you just motivated me to rewrite the code to something more modern .. Will update the post soon.