Sequential running of tasks with a cancellation token
npm install @rozhkov/one-task
# or
yarn add @rozhkov/one-task
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
interface ICancellationToken {
readonly isCanceled: boolean;
}
type Task = (token: ICancellationToken) => Promise<void>;
type OneTask = (task: Task) => void;
Rozhkov One Task is MIT licensed, as found in the LICENSE file.