Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
mutable-promise
Advanced tools
Wrapper for Promise. Resolvable, rejectable, redirectable.
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.14/dist/index.esm.min.js';
</script>
<script src="https://cdn.jsdelivr.net/npm/mutable-promise@1.1.14/dist/index.min.js"></script>
resolve
outside of the executor.
Get status
of the promise
.
// can use like a native `Promise`
let p = new MutablePromise((_, rj)=>setTimeout(rj,100)); // try to reject after 100ms
// can get status
console.log(p.status); // "pending"
console.log(p.isPending); // true
// can resolve outside
p.then(anything=>console.log(anything)); // 'resolve called'
p.resolve('resolve called'); // resolve immediately
console.log(p.status); // "fulfilled"
console.log(p.isFulfilled); // true
(async()=>{
await new Promise(rs=>setTimeout(rs,200)); // wait 200ms
// status will not change after `fulfilled` or `rejected`
console.log(p.status); // "fulfilled"
console.log(p.isFulfilled); // true
})();
reject
outside of the executor.
let p = new MutablePromise(rs=>setTimeout(rs,100)); // try to resolve after 100ms
// can reject outside
p.catch(anything=>console.log(anything)); // 'reject called'
p.reject('reject called'); // reject immediately
console.log(p.status); // "rejected"
console.log(p.isRejected); // true
Allow setting a promise
as argument.
// simple promise
new MutablePromise(Promise.resolve('1'));
// typical promise
let nativeP = new Promise(rs=>setTimeout(rs,200));
new MutablePromise(nativeP);
// wrapper a fetch
new MutablePromise(fetch('./'));
// nested MutablePromise
let p = new MutablePromise(rs=>setTimeout(rs,200));
new MutablePromise(p);
// promise like
let pLike = { then: function(){ return 'a'; } };
new MutablePromise(pLike);
Allow setting no argument or null
. Then define a task
later.
let p = new MutablePromise(); // or `new MutablePromise(null)`
p.then(anything=>console.log(anything)); // 'msg from later define task'
// The property `task` can accept the same parameter type as the constructor of `MutablePromise`
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)); // 'new task'
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)); // 'resolved'
(async()=>{
// wait next js event loop
await new Promise(rs=>setTimeout(rs,0));
console.log(p.status); // "fulfilled"
p.catch(anything=>console.log(anything)); // will not 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)); // will not log anything
p.task = null;
// the promise will keep `pending`
setTimeout(()=>{
console.log(p.status);
},200)
// you can define a new `task` later
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)); // wait next js event loop
console.log(p.status); // "fulfilled"
p = new MutablePromise(resolve=>setTimeout(()=>{resolve('you can define a new `MutablePromise` instead')},100));
p.then(anything=>console.log(anything));
})();
Maybe need to wrap other promise function like all
, resolve
, race
and so on, to MutablePromise
edition.
FAQs
Wrapper for Promise. Resolvable, rejectable, redirectable.
The npm package mutable-promise receives a total of 3,687 weekly downloads. As such, mutable-promise popularity was classified as popular.
We found that mutable-promise demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.