Skip to content

Commit

Permalink
Merge pull request #234 from mmikita95/patch-1
Browse files Browse the repository at this point in the history
Update event-handlers.md
  • Loading branch information
ramedina86 authored Feb 12, 2024
2 parents faf8b55 + 5381876 commit 9e001d6
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions docs/docs/event-handlers.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,25 @@ def handle_slowly(state):
```

The code above will set `message` to "Loading...", then to "Completed".

## Asynchronous Event Handlers

Streamsync supports asynchronous event handlers, allowing for non-blocking I/O operations directly within event handlers. This is particularly useful for tasks such as fetching data from a database, making HTTP requests, or performing any other I/O bound operation that can benefit from asynchronous execution.

### Defining an Asynchronous Handler

An asynchronous event handler is defined with the standard `async` keyword syntax.

```py
# An asynchronous event handler for performing an I/O bound operation
async def handle_async_click(state):
data = await fetch_data()
state["data"] = data
```

In the example above, `fetch_data()` is an asynchronous function that retrieves data, potentially from a remote source. The `await` keyword is used to wait for the operation to complete without blocking the main thread, allowing other tasks to run concurrently.

### Awaitable Objects

You can use any awaitable object within an async event handler. This includes the output of any function defined with `async def`, or objects with an `__await__` method. This makes it easy to integrate with asynchronous libraries and frameworks.

0 comments on commit 9e001d6

Please sign in to comment.