☎️ React hook for managing async tasks/coroutines (with cancellation)
npm install react-use-task☎️ React hook for managing async tasks/coroutines (with cancellation)




True cancellation requires generator functions. This library was inspired by ember-concurrency and uses posterus to run generators as coroutines.
``jsx
import { useTask } from 'react-use-task'
const Demo = () => {
const [
{ isRunning, performCount, last, lastSuccessful },
perform,
cancelAll
] = useTask(function* () {
yield new Promise(r => setTimeout(r, 1000));
return +new Date;
});
return (
$3
`jsx
import { useWorker } from 'react-use-task'const Demo = () => {
useWorker(function* () {
while (true) {
console.log('tick');
yield new Promise(r => setTimeout(r, 1000));
}
});
return (
Worker task will be automatically cancelled when component is unmounted
)
}
`Plans
* Non-immediate running of task (e.g. on button press)
*
.drop, .restartable, .maxConcurrency task modifiers (like ember-concurrency)
* Exposing manual cancellation
* Historical state - last, lastSuccessful (like e-c`)