vue-reactive-stateful-promise
v1.0.5
Published
Extends promises with their state, a timeout and make them cancellable, all of that vue reactive
Downloads
47
Maintainers
Readme
vue-reactive-stateful-promise
Native Promise wrapper with reactive state getters
(isPending
, isResolved
, isRejected
).
Installation
npm install vue-reactive-stateful-promise
Usage
import StatefulPromise from 'vue-reactive-stateful-promise';
Constructor
Create a new promise the same way as you would with the Promise
constructor:
let p = new StatefulPromise((res, rej) => {
// ...
});
p.isPending; // true
p.isResolved; // false
p.isRejected; // false
Pass in an async function:
let p = new StatefulPromise(async () => {
// ...
});
p.isPending; // true
p.isResolved; // false
p.isRejected; // false
Extend an existing promise:
let pr = new Promise((resolve, reject) => {
setTimeout(resolve, 10);
});
let p = new StatefulPromise(pr);
p.isPending; // true
p.isResolved; // false
p.isRejected; // false
Also works with Promise
static methods:
let p = StatefulPromise.resolve('foo');
p.isPending; // false
p.isResolved; // true
p.isRejected; // false
Options
The constructor also accept options
let p = new StatefulPromise((res, rej) => {
setTimeout(res, 2000);
}, {timeout: 1000, ignoreAbort: true});
p.abort(); // This will do nothing
p.catch((e) => {
console.error(e); // Will return Error: Promise timed out
});
License
This software is released under the terms of the GPL 3.0 license. See LICENSE
.