@rozhkov/one-task
v1.0.2
Published
Sequential running of tasks with a cancellation token
Downloads
5
Maintainers
Readme
One Task
Sequential running of tasks with a cancellation token
Installation
npm install @rozhkov/one-task
# or
yarn add @rozhkov/one-task
Usage
import oneTask from '@rozhkov/one-task';
const updateStateAsTransaction = async (cancellationToken) => {
const rollback = async () => {/* restore state */};
// asynchronous complex update
if (cancellationToken.isCanceled) {
await rollback();
return;
}
// asynchronous complex update
}
const run = oneTask();
run(updateStateAsTransaction); // it will be called, but the token will be canceled
run(updateStateAsTransaction); // this task will be skipped
run(updateStateAsTransaction); // it will be run after completing the first task
API
interface ICancellationToken {
readonly isCanceled: boolean;
}
type Task = (token: ICancellationToken) => Promise<void>;
type OneTask = (task: Task) => void;
👨💻 Author
📄 License
Rozhkov One Task is MIT licensed, as found in the LICENSE file.