react-use-task
☎️ React hook for managing async tasks/coroutines (with cancellation)
Why ?
True cancellation requires generator functions. This library was inspired by ember-concurrency and uses posterus to run generators as coroutines.
Usage
Tasks
import useTask from 'react-use-task' const Demo = const isRunning performCount last lastSuccessful perform cancelAll = ; return <div> <button => Perform Task </button> <button => Cancel All </button> <div> isRunning: '' + isRunning Perform count: performCount </div> <div> last: JSON </div> <div> last success value: lastSuccessful ? lastSuccessfulvalue : '<none>' </div> </div>
Workers (background tasks)
import useWorker from 'react-use-task' const Demo = ; return <div> <div>Worker task will be automatically cancelled when component is unmounted</div> </div>
Plans
- Non-immediate running of task (e.g. on button press)
.drop
,.restartable
,.maxConcurrency
task modifiers (likeember-concurrency
)- Exposing manual cancellation
- Historical state -
last
,lastSuccessful
(likee-c
)