You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/async/README.md
+36-4Lines changed: 36 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -41,11 +41,41 @@ For simple cases of turning plain async function to `AsyncAction`-s, you can use
41
41
42
42
### Hooks
43
43
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(asyncctx=> {
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
+
```
45
75
46
76
### Prologue to examples
47
77
48
-
In examples below, the following helper will be used:
78
+
In examples below, the following helper will be used for making HTTP requests:
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).
0 commit comments