@rozhkov/one-task
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

One Task

Rozhkov One Task tests Current npm package version.

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

Sergey Rozhkov

📄 License

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

Package Sidebar

Install

npm i @rozhkov/one-task

Weekly Downloads

1

Version

1.0.2

License

MIT

Unpacked Size

11.3 kB

Total Files

28

Last publish

Collaborators

  • rozhkov