Skip to content

Commit d151355

Browse files
author
krulod
committed
docs: add reatomAsync hooks docs
1 parent 6e270b4 commit d151355

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/async/README.md

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,41 @@ For simple cases of turning plain async function to `AsyncAction`-s, you can use
4141

4242
### Hooks
4343

44-
TODO
44+
`AsyncAction` has several action fields you can subscribe to with `onCall`. They are called automatically when a promise returned by user code is settled:
45+
46+
- `onFulfill`: called when promise is resolved, like `Promise.then`
47+
- `onReject`: called when promise is rejected, like `Promise..catch`
48+
- `onSettle`: called when promise is either resolved or rejected, like `Promise..finally`
49+
50+
Example:
51+
52+
```ts
53+
const reatomDoSomething = reatomAsync(async ctx => {
54+
// ...
55+
}, 'reatomDoSomething')
56+
57+
let status: 'n/a' | 'pending' | 'resolved' | 'rejected' = 'n/a'
58+
59+
reatomDoSomething.onCall((ctx) => {
60+
status = 'pending'
61+
})
62+
63+
reatomDoSomething.onSettle.onCall((ctx) => {
64+
// ...
65+
})
66+
67+
reatomDoSomething.onFulfill.onCall((ctx) => {
68+
status = 'resolved'
69+
})
70+
71+
reatomDoSomething.onReject.onCall((ctx) => {
72+
status = 'rejected'
73+
})
74+
```
4575

4676
### Prologue to examples
4777

48-
In examples below, the following helper will be used:
78+
In examples below, the following helper will be used for making HTTP requests:
4979

5080
```ts
5181
async function request<T>(...args: Parameters<typeof fetch>): Promise<T> {
@@ -69,7 +99,7 @@ export const fetchList = reatomAsync(
6999

70100
### Advanced example
71101

72-
Now we are going to solve a more complex task: remember the last fetched list state and error if the request failed, cancel previous requests when making subsequent ones and add an action to mutate the list. The next snippet assumes that you are familiar with [`withAssign`] and [`atom..onCall`].
102+
Now we are going to solve a more complex task: remember the last fetched list state and error if the request failed, cancel previous requests when making subsequent ones and add an action to mutate the list. The next snippet assumes that you are familiar with [`withAssign`](https://reatom.dev/package/primitives#withassign) and [`action..onCall`](https://reatom.dev/package/core#actiononcall-api).
73103

74104
```ts
75105
import { reatomAsync } from '@reatom/async'
@@ -144,7 +174,9 @@ type Element = {
144174

145175
export const fetchList = reatomAsync(
146176
(ctx, page: number) =>
147-
request<Array<Element>>(`/api/list?page=${page}`, {signal:ctx.controller.signal}),
177+
request<Array<Element>>(`/api/list?page=${page}`, {
178+
signal: ctx.controller.signal,
179+
}),
148180
'fetchList',
149181
).pipe(withDataAtom([]), withErrorAtom(), withAbort(), withStatusesAtom())
150182

packages/async/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ export {
5151

5252
export interface AsyncAction<Params extends any[] = any[], Resp = any>
5353
extends Action<Params, ControlledPromise<Resp>> {
54+
/** @deprecated use anAction.onFulfill.onCall */
5455
onFulfill: Action<[Resp], Resp>
56+
/** @deprecated use anAction.onReject.onCall */
5557
onReject: Action<[unknown], unknown>
58+
/** @deprecated use anAction.onSettle.onCall */
5659
onSettle: Action<[], void>
5760
pendingAtom: Atom<number>
5861

0 commit comments

Comments
 (0)