GithubHelp home page GithubHelp logo

kevinokerlund / cancellable-promise Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 3 KB

An extension of native ES6 Promises that provides the ability to cancel and/or clear promise chains.

License: MIT License

JavaScript 100.00%
native-es6-promises clear-promise-chains promise-cancelling promise-chain promise promises es6-promise javascript-promise

cancellable-promise's Introduction

cancellable-promise

An extension of native ES6 Promises that provides the ability to cancel and clear promise chains.

Usage

Creating a new cancellable promise

Cancellable promises are created just like regular promises, the only difference is the constructor you use.

let promise = new CancellablePromise((resolve, reject) => {
	setTimeout(() => {
		resolve('Resolved!');
	}, 500)
});

promise
    .then(val => console.log(val))
    .catch(val => console.log(val));

Cancelling a promise

Cancelling a promise, internally calls the "reject" on the promise.

promise.cancel();

Clearing a promise chain

Sometimes it is helpful to just clear all the .then, and .catch promise chains. This method clears whatever has been set in the past.

promise.clear();

Common questions

Will a cancellable promise still work with Promise.race, and Promise.all?

Yes. This extension literally extends Promise.

When I call cancel on a promise, will it cause an error in the browser?

.cancel internally calls the rejection callback and so it will default to however the browser treats a rejected promise for ES6. Chrome for example will throw an error if a rejection is called on a promise, and there is no .catch. Firefox and Safari on the other hand, will not.

cancellable-promise's People

Contributors

kevinokerlund avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.