1.0.0 • Published 6 months ago

one-t-experiment v1.0.0

Weekly downloads
-
License
-
Repository
github
Last release
6 months ago

🥇 One Task (Test) 🥇

Sequential running of tasks with a cancellation token

Installation

npm install one-task
# or
yarn add one-task

Usage

import oneTask from '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

Sergey Rozhkov

📄 License

Rozhkov One Task is MIT licensed, as found in the LICENSE file.