npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mutable-promise

v1.1.14

Published

Wrapper for Promise. Resolvable, rejectable, redirectable.

Downloads

21,253

Readme

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/[email protected]/dist/index.esm.min.js';
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.min.js"></script>

Example

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));
})();

TODO

Maybe need to wrap other promise function like all, resolve, race and so on, to MutablePromise edition.