> For the complete documentation index, see [llms.txt](https://docs.conva.ai/slang/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.conva.ai/slang/advanced-topics/asynchronous-action-handling.md).

# Asynchronous Action Handling

{% hint style="info" %}
This section applies only to Android, ReactNative, and Flutter platforms
{% endhint %}

While handling a user journey, the app might not be able to identify the state or the condition of the journey in a synchronous way. For example, the app could open a new activity (or page) which could then fire up a search to the back-end and show the results. So the callback itself would not be aware of the result unless it blocks till the entire activity is loaded. This is not desirable and leads to poor UX.

To solve this problem, the Slang Assistant programming model provides ways for the app to asynchronously signal the Assistant about the app state changes and conditions.&#x20;

This can be done by using the "*wait*" and "*notify*" semantics. The process is quite straightforward. In order to deal with asynchronous operations within the callback:

* The app must return the `AppState` **`WAITING`** to inform the Assistant that it's not yet ready to identify the state or the condition of the app. The Assistant goes into a "waiting" state without blocking the UI or other threads and continues to wait for further notification from the app.
* When the app has completed the asynchronous operation, it can then call `notifyAppState` to notify the Assistant to proceed further.

### Returning `WAITING` `AppState`

The app should return WAITING from the user journey callback. This would keep the Assistant in a "processing" (which is what it would have been when it invoked the callback).&#x20;

{% tabs %}
{% tab title="Android Native" %}

```
public SearchUserJourney.AppState onSearch(SearchInfo searchInfo, SearchUserJourney searchJourney) {        
    // Fire an async search request
    // ...
    return SearchUserJourney.AppState.WAITING;
}
```

{% endtab %}

{% tab title="React Native" %}

```
onSearch: async (searchInfo, searchUserJourney) => {
    // Fire an async search request
    // ...
        
    return SlangRetailAssistant.SearchAppState.WAITING;
  },
```

{% endtab %}

{% tab title="Flutter" %}

```
SearchAppState onSearch(
      SearchInfo searchInfo, SearchUserJourney searchUserJourney) {     
    // Fire an async search request
    // ...
    return SearchAppState.WAITING;
}
```

{% endtab %}
{% endtabs %}

### Notifying the Assistant

Once the Assistant is asked to wait, it will continue to remain in the "*processing*" state until the app notifies it to stop waiting, or the user cancels the operation through the UI. For best results, the app should ensure that every path that returns `WAITING` has an accompanying call to notify the Assistant.&#x20;

To notify the Assistant with the `AppState`, the app needs access to the `UserJourney` object that was passed to the callback. This can be accessed via a global helper method `getLastSearchUserJourney` and then call `notifyAppState` on that object.

{% tabs %}
{% tab title="Android Native" %}

```
SearchUserJourney userJourney = SlangRetailAssistant.getLastSearchUserJourney();

userJourney.setSearchSuccess();
userJourney.notifyAppState(SearchUserJourney.AppState.SEARCH_RESULTS);
```

{% endtab %}

{% tab title="React Native" %}

```
SearchUserJourney userJourney = SlangRetailAssistant.getLastSearchUserJourney();

userJourney.setSearchSuccess();
userJourney.notifyAppState(SearchUserJourney.AppState.SEARCH_RESULTS);
```

{% endtab %}

{% tab title="Flutter" %}

```
SearchUserJourney userJourney = SlangRetailAssistant.getLastSearchUserJourney();

userJourney.setSuccess();
userJourney.notifyAppState(SearchAppState.SEARCH_RESULTS);
```

{% endtab %}
{% endtabs %}

##
