Skip to content

Eazymov/cancel.it

Repository files navigation

cancel.it

npm License Build Status Coverage Status types: typescript/flow code style: prettier PRs Welcome

Cancelable promise | github.com/Eazymov/cancel.it

Installation

Direct <script /> include

The library will be exposed as a global Cancelable variable

<script src="https://cdn.jsdelivr.net/npm/cancel.it@latest"></script>

or via unpkg

<script src="https://unpkg.com/cancel.it@latest"></script>

NPM

npm install cancel.it --save

Yarn

yarn add cancel.it

About

A promise wrapper that provides possibility to create cancelable promises and wrap existing ones. Supports both TypeScript and Flow type annotations out of the box. Commonjs and es6-modules compatible

Usage

import Cancelable from 'cancel.it';

// This
const promise = new Promise((resolve, reject) => {
  setTimeout(() => {
    someCondition ? resolve(someValue) : reject(reason);
  }, 1000);
});

// Turns into this
const cancelable = new Cancelable((resolve, reject) => {
  setTimeout(() => {
    someCondition ? resolve(someValue) : reject(reason);
  }, 1000);
});

// OR
const cancelable = Cancelable.from(promise);

cancelable.isCanceled(); // false
cancelable.cancel();
cancelable.isCanceled(); // true

// Will never be fired
cancelable.then(/* ... */);
cancelable.catch(/* ... */);
cancelable.finally(/* ... */);

Questions

If you have any troubles, questions or proposals you can create the issue

License

MIT

Copyright (c) 2019 - present, Eduard Troshin