# 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 %}

##


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.conva.ai/slang/advanced-topics/asynchronous-action-handling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
