MutablePromise
Wrapper for Promise. Resolvable, rejectable, redirectable.
Import
install
npm i mutable-promise -S
yarn add mutable-promise
import
import MutablePromise from 'mutable-promise';
const MutablePromise = require('mutable-promise');
CDN
<script type="module">
import MutablePromise from 'https://cdn.jsdelivr.net/npm/mutable-promise@1.1.8/dist/index.esm.min.js';
</script>
<script src="https://cdn.jsdelivr.net/npm/mutable-promise@1.1.8/dist/index.min.js"></script>
Example
resolve
outside of the executor.
Get status
of the promise
.
let p = new MutablePromise((_, rj)=>setTimeout(rj,100));
console.log(p.status);
console.log(p.isPending);
p.then(anything=>console.log(anything));
p.resolve('resolve called');
console.log(p.status);
console.log(p.isFulfilled);
(async()=>{
await new Promise(rs=>setTimeout(rs,200));
console.log(p.status);
console.log(p.isFulfilled);
})();
reject
outside of the executor.
let p = new MutablePromise(rs=>setTimeout(rs,100));
p.catch(anything=>console.log(anything));
p.reject('reject called');
console.log(p.status);
console.log(p.isRejected);
Allow setting a promise
as argument.
new MutablePromise(Promise.resolve('1'));
let nativeP = new Promise(rs=>setTimeout(rs,200));
new MutablePromise(nativeP);
new MutablePromise(fetch('./'));
let p = new MutablePromise(rs=>setTimeout(rs,200));
new MutablePromise(p);
let pLike = { then: function(){ return 'a'; } };
new MutablePromise(pLike);
Allow setting no argument or null
. Then define a task
later.
let p = new MutablePromise();
p.then(anything=>console.log(anything));
p.task = rs=>rs('msg from later define task');
Can change task
before fulfilled
or rejected
.
let p = new MutablePromise(rs=>setTimeout(()=>{rs('original task')},100));
p.then(anything=>console.log(anything));
p.task = new MutablePromise(rs=>setTimeout(()=>{rs('new task')},200));
A change make no sense after fulfilled
or rejected
.
let p = new MutablePromise(Promise.resolve('resolved'));
p.then(anything=>console.log(anything));
(async()=>{
await new Promise(rs=>setTimeout(rs,0));
console.log(p.status);
p.catch(anything=>console.log(anything));
p.task = Promise.reject('make no sense');
await new Promise(rs=>setTimeout(rs,0));
p.task = (rs)=>{
console.log('function still run after `fulfilled` or `rejected`');
rs('but will not resolve or reject');
};
})();
Set task
as null
can cancel the orignial task.
let p = new MutablePromise(resolve=>setTimeout(()=>{
console.log('the executor will run anyway');
resolve('original task');
},100));
p.then(anything=>console.log(anything));
p.task = null;
setTimeout(()=>{
console.log(p.status);
},200)
If a MutablePromise
has been fulfilled
or rejected
, you can define a new MutablePromise
instead.
(async()=>{
let p = new MutablePromise(Promise.resolve());
await new Promise(rs=>setTimeout(rs,0));
console.log(p.status);
p = new MutablePromise(resolve=>setTimeout(()=>{resolve('you can define a new `MutablePromise` instead')},100));
p.then(anything=>console.log(anything));
})();
TODO
Maybe need to wrap other promise function like all
, resolve
, race
and so on, to MutablePromise
edition.