1.0.5 • Published 4 years ago
vue-reactive-stateful-promise v1.0.5
vue-reactive-stateful-promise
Native Promise wrapper with reactive state getters
(isPending, isResolved, isRejected).
Installation
npm install vue-reactive-stateful-promiseUsage
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; // falsePass in an async function:
let p = new StatefulPromise(async () => {
// ...
});
p.isPending; // true
p.isResolved; // false
p.isRejected; // falseExtend 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; // falseAlso works with Promise static methods:
let p = StatefulPromise.resolve('foo');
p.isPending; // false
p.isResolved; // true
p.isRejected; // falseOptions
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.