4
4
![ David] ( https://img.shields.io/david/DigitalBrainJS/use-async-effect )
5
5
[ ![ Stars] ( https://badgen.net/github/stars/DigitalBrainJS/use-async-effect )] ( https://github.com/DigitalBrainJS/use-async-effect/stargazers )
6
6
7
- ## useAsyncEffect2
7
+ ## useAsyncEffect2 : snowflake :
8
8
The library provides a React hook with ability to automatically cancel asynchronous code inside it.
9
9
It just makes it easier to write cancelable asynchronous code that doesn't cause
10
10
the following React issue when unmounting, if your asynchronous tasks that change state are pending:
@@ -16,7 +16,7 @@ To fix, cancel all subscriptions and asynchronous task in "a useEffect cleanup f
16
16
It uses [ c-promise2] ( https://www.npmjs.com/package/c-promise2 ) to make it work.
17
17
When it used in conjunction with other libraries that work with the CPromise,
18
18
such as [ cp-fetch] ( https://www.npmjs.com/package/cp-fetch ) and [ cp-axios] ( https://www.npmjs.com/package/cp-axios ) ,
19
- you get a powerful tool for building asynchronous logic for your components.
19
+ you get a powerful tool for building asynchronous logic of your React components.
20
20
You just have to use ` generators ` instead of an async function to make your code cancellable,
21
21
but basically, that just means you will have to use ` yield ` instead of ` await ` keyword.
22
22
## Installation :hammer :
@@ -50,7 +50,7 @@ function JSONViewer(props) {
50
50
return < div> {text}< / div> ;
51
51
}
52
52
````
53
- Example with timeout & error handling ([ Live demo] ( https://codesandbox.io/s/async-effect-demo1-vho29 ) ):
53
+ Example with a timeout & error handling ([ Live demo] ( https://codesandbox.io/s/async-effect-demo1-vho29 ) ):
54
54
```` jsx
55
55
import React from " react" ;
56
56
import {useState } from " react" ;
@@ -88,13 +88,65 @@ export default function TestComponent(props) {
88
88
return < div> {text}< / div> ;
89
89
}
90
90
````
91
+ useAsyncCallback example ([ Live demo] ( https://codesandbox.io/s/use-async-callback-bzpek?file=/src/TestComponent.js ) ):
92
+ ```` javascript
93
+ import React from " react" ;
94
+ import {useState } from " react" ;
95
+ import {useAsyncCallback } from " use-async-effect2" ;
96
+ import {CPromise } from " c-promise2" ;
97
+
98
+ export default function TestComponent (props ) {
99
+ const [text , setText ] = useState (" " );
100
+
101
+ const asyncRoutine = useAsyncCallback (function * (v ){
102
+ setText (` Stage1` );
103
+ yield CPromise .delay (1000 );
104
+ setText (` Stage2` );
105
+ yield CPromise .delay (1000 );
106
+ setText (` Stage3` );
107
+ yield CPromise .delay (1000 );
108
+ setText (` Done` );
109
+ return v;
110
+ })
111
+
112
+ const onClick = ()=> {
113
+ asyncRoutine (123 ).then (value => {
114
+ console .log (` Result: ${ value} ` )
115
+ }, console .warn );
116
+ }
117
+
118
+ return < div>< button onClick= {onClick}> Run async job< / button>< div> {text}< / div>< / div> ;
119
+ }
120
+ ````
121
+
91
122
To learn more about available features, see the c-promise2 [ documentation] ( https://www.npmjs.com/package/c-promise2 ) .
92
123
93
124
## Playground
94
125
95
126
To get it, clone the repository and run ` npm run playground ` in the project directory or
96
127
just use the codesandbox [ demo] ( https://codesandbox.io/s/async-effect-demo1-vho29 ) to play with the library online.
97
128
129
+ ## API
130
+
131
+ ### useAsyncEffect(generatorFn, deps?): (function cancel(): boolean )
132
+ A React hook based on [ ` useEffect ` ] ( https://reactjs.org/docs/hooks-effect.html ) , that resolves passed generator as asynchronous function.
133
+ The asynchronous generator sequence and its promise of the result will be canceled if
134
+ the effect cleanup process is started before it completes.
135
+ The generator can return a cleanup function similar to the ` useEffect ` hook.
136
+ - ` generatorFn(...userArgs, scope: CPromise) ` : ` GeneratorFunction ` - generator to resolve as an async function.
137
+ The last argument passed to this function and ` this ` refer to the CPromise instance.
138
+ - ` deps?: any[] ` - effect dependencies
139
+
140
+ ### useAsyncCallback(generatorFn, deps?): CPromiseAsyncFunction
141
+ ### useAsyncCallback(generatorFn, options?: object): CPromiseAsyncFunction
142
+ This hook makes an async callback that can be automatically canceled on unmount or by user request.
143
+ #### options:
144
+ - ` deps: any[] `
145
+ - ` combine:boolean ` - allow only single thread running.
146
+ All subsequent callings will return promises that subscribed to the pending promise of the first call.
147
+ - ` cancelPrevious:boolean ` - cancel the previous pending async function before running a new one.
148
+ - ` concurrency: number=0 ` - set concurrency limit for simultaneous calls.
149
+
98
150
## Related projects
99
151
- [ c-promise2] ( https://www.npmjs.com/package/c-promise2 ) - promise with cancellation, decorators, timeouts, progress capturing, pause and user signals support
100
152
- [ cp-axios] ( https://www.npmjs.com/package/cp-axios ) - a simple axios wrapper that provides an advanced cancellation api
0 commit comments