How do I run queries or APIs in sequence using promises?

How to run queries/APIs and multiple chained actions in a particular sequence?

Appsmith supports JavaScript promises, with this one can write chained actions or queries in a particular sequence.

Following is an example code snippet:

export default {
// function start 
fetch_with_callback: () => {
    showAlert('Before calling fetch movies query using promise');
    fetchMovies.run(
      (res) => showAlert('Successfully fetched movies data: found ' + res.length + ' rows.'),
      (e) =>  showAlert('Failed to fetch movies data' + e)
    );
    showAlert('After calling fetch movies query using promise');
  },
// function end 

}

In this snippet:

  • First, it shows an alert Before calling fetch movies query using promise
  • Next, it runs a query named fetchMovies, while handling errors
  • Last, it shows one more alert saying After calling fetch movies query using promise

To learn more about Promises on Appsmith, read the documentation here:

The following question is also discussed on How Do I Do X on Appsmith Session on our community call. Watch it here: How Do I Do X? - YouTube