0.1.0 • Published 4 years ago

breakable-promise v0.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

THIS PROJECT IS CURRENTLY UNDER DEVELOPMENT

Breakable Promise

A "Promise" implementation that can be broken.

Install

npm i breakable-promise

How does it work?

Breakable Promise exposes the same API and works like a regular Promise (except that it can be broken).

const promise = new BreakablePromise((resolve, reject, breakPromise) => {
    const timeoutVar = setTimeout(() => {
        if (Math.round(Math.random() * 1000) > 500) {
            resolve('Limit achieved');
        } else {
            reject('Limit not reached');
        }
    }, 3000);
    breakPromise(() => {
        clearTimeout(timeoutVar);
    });
});

promise
    .then((result) => console.log(result))
    .catch((error) => console.log(error));

const timerVar = Math.round(Math.random() * 10000);
if (timerVar < 3000) {
    promise.break()
        .then(() => {
            console.log('Promise was successfully broken');
        })
        .catch(() => {
            console.log('Promise can no longer be broken');
        });
}