Cancel an Asynchronous Request in a Flow Local Action

If an asynchronous request times out, the flow executes the local action’s fault connector and sets the error message to $Flow.FaultMessage. However, the original request isn’t automatically canceled. To abort an asynchronous request, use the cancelToken parameter available in the invoke() method.

By default, requests time out after 120 seconds.

Note

Example 

In this local action component class, the invoke() method returns a Promise. When the method has done all it needs to do, it fulfills the Promise and control returns to the flow.

  • If the request is successful, the method uses resolve() to execute the next element in the flow after this action.
  • If the request isn’t successful, it uses reject() to execute the local action’s fault connector and sets $Flow.FaultMessage.
  • If the request takes too long, it uses cancelToken.promise.then to abort the request.
1import { api, LightningElement } from 'lwc';
2import { ShowToastEvent } from 'lightning/platformShowToastEvent';
3
4
5export default class ShowToastExampleComponent extends LightningElement {
6   @api toastTitle;
7   @api toastMessage;
8
9
10   @api invoke(cancelToken) {
11       return new Promise((resolve, reject) => {
12
13
14           // If the Promise times out, abort the request and
15           // pass set $Flow.FaultMessage to "Request timed out"
16           cancelToken.promise.then(error => {
17// Here is where you can clean up ongoing async requests and customize the fault message in the event of a timeout
18               reject(new Error("Request timed out."));
19           });
20
21           // Do your asynchronous work and call resolve() or reject("Custom error message") when finished.
22           resolve();
23       });
24   }

See Also