Skip to content

Commit

Permalink
add section 'Async effects'
Browse files Browse the repository at this point in the history
  • Loading branch information
benStre committed Jan 19, 2024
1 parent 8d50e14 commit a3d478f
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions docs/manual/03 Pointers.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,32 @@ it is garbage colleted and the effect is removed.

Weak value bindings can be used with all *object* values, not just with pointers.

### Async effects

Effect callbacks cannot be `async` functions.
To handle async operations, you can always call an async function from inside the
effect callback:

```ts
const searchName = $$("");
const searchAge = $$(18);

// async function that searches for a user and shows the result somewhere
async function searchUser(name: string, age: number) {
const user = await query({type: "user", name, age});
showUser(user);
}

// effect that triggers the user search every time searchName or searchAge is changed
effect(() => searchUser(searchName.val, searchAge.val))
```

All dependency values of the effect must be accessed synchronously.
This means that the variables inside the async function don't trigger the effect, only the ones passed
into the `searchUser` call.



## Observing pointer changes

For more fine grained control, the `observe()` function can be used to handle pointer value updates.
Expand Down

0 comments on commit a3d478f

Please sign in to comment.