0

I have a Lightning Datatable with row actions. Clicking on one the row actions need to make an external callout and get values for another function which dispatches an event with those retrieved values. Like this:

case GenerateReport:
            this.selectedRow = event.detail.row;
            this.getDataFromAPI(); //Uses apexContinuation for callouts
            this.dispatchDataForReport();
            break;

Both the JS functions work well, but not in sync. I've tried searching online for a similar use-case, but everywhere it's unique. How can implement a Promise(or async/await) approach to get the API data prior to calling the dispatch function?

1 Answer 1

4

The getDataFromApi method is a promise, so you can do this using the import name you are using for the Apex continuation call. Let's say it's called the same name.

getDataFromApi().then((result) => {
    // Then you can call your other method. Not sure what this is returning.
}).catch((error) => {
    // error handling
});

The other option is to you an Async function that you can call from your action. I prefer this option if you are using multiple Apex calls.

// Change the function name to whatever is more identifying
async rowActionHandler() {
    try {
        // do what you need to do with this.selectedRow
        const continuationResult = await getDataFromApi();
        // Do what you need to do with the result if at all. If you don't need a result just call awati
        // If you have another apex method you can use await again
        const apexResult = await dispatchDataForReport();
    } catch (error) {
        // error handling
    }
}
1
  • Approach #1 worked for me. I appreciate your quick response. Dec 9, 2021 at 21:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .