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

balle

v1.0.47

Published

Simple isomorphic promise implementation

Downloads

19

Readme

Coverage Status

Package Quality

WTF ?

No... the world does not need that shit but I need to try to understand.

Just to be clear, this implementation has nothing to do with A+ promise specs


install & test

@ yarn
@ yarn test 
@ yarn cover

usage

Make a promise :

const Balle = require('balle');
const p = new Balle((resolve, reject) => {
    var before = +new Date;
    setTimeout(() => 
        Math.random() > .5
            ? resolve([before, +new Date])
            : reject('that`s the cause')
    , 2000);
})

// deal with success using then
.then(result => console.log(result))

// deal with rejection | thrown error using catch
.catch(whatever => {
    console.log('Failure:');
    console.log(whatever);
})

// do something anyway
.finally(result_cause_error => {
    // get the result in case on resolution or the cause
    // in case of rejection|error
    console.log('Executed regardless the resolution or rejection')
});

reject a promise:

const p = new Balle((resolve, reject) => 
    setTimeout(() => reject('Ups... something went wrong'), 1000)
)
.then(() => { throw 'never thrown'; })
.catch((cause) => 
    // this will log
    console.log(cause)
);

late launch:

const resolvingPromise = new Balle();

resolvingPromise
.then(() => {
    throw 'Never executed';
})
.catch((cause) => {
    console.log('catched: ' + cause);
}).finally(cause => console.log('finally : ' + cause));

resolvingPromise
.launch((resolve, reject) => 
    setTimeout(function () {
        reject('a problem occurred');
    }, 100)
);

resolve:

const resolvingPromise = new Balle();
resolvingPromise.resolve('the value');
resolvingPromise.then(v => console.log(v === 'the value'));

reject:

const rejectingPromise = new Balle();
rejectingPromise.reject('the cause');
rejectingPromise.catch(v => console.log(v === 'the cause'));

Balle.one

// wraps the constructor call
const p1 = new Balle(/* executor func */);
// can be written
const p1 = Balle.one(/* executor func */);

Balle.all

const init = +new Date;
const p = Balle.all([
    Balle.one((resolve, reject) => 
        setTimeout(() => resolve(500), 1000)
    ),
    Balle.one((resolve, reject) => 
        setTimeout(() => resolve(200), 2000)
    ),
    Balle.one((resolve, reject) => 
        setTimeout(() => resolve(300), 1500)
    ),
])
.then((result) => {
    console.log((+new Date - init)+ ' ≈ 2000');
    console.log(result); // ---> [500, 200, 300]
})
.catch(() => {
    throw 'never thrown';
});

Balle.race

const init = +new Date;
const p = Balle.race([
    Balle.one((resolve, reject) => 
        setTimeout(() => resolve(500), 1000)
    ),
    Balle.one((resolve, reject) => 
        setTimeout(() => resolve(200), 1500)
    ),
    Balle.one((resolve, reject) => 
        setTimeout(() => resolve(300), 2000)
    ),
])
.then((result) => {
    console.log((+new Date - init) + ' ≈ 1000');
    console.log(result + ' == 500'); 
})
.catch(() => {
    throw 'never thrown';
});

Balle.chain

Balle.chain([
    () => Balle.one((resolve, reject) => 
        setTimeout(() => 
            Math.random() > .5
                ? reject('a problem occurred at #1')
                : resolve(100)
        , 100)
    ),
    r => Balle.one((resolve, reject) => 
        setTimeout(() => 
            Math.random() > .5
                ? reject('a problem occurred at #2')
                : resolve(101 + r)
        , 200)
    ),
    r => Balle.one((resolve, reject) => 
        setTimeout(() => 
            Math.random() > .5
                ? reject('a problem occurred at #3')
                : resolve(102 + r)
        , 300)
    )
])
.then(r => console.log('result : '+ r))
.catch(cause => console.log('cause : '+ cause))
.finally(() => console.log('----- finally -----'));

Balle.all async errors

Balle.all([
    Balle.one((res, rej) => 
        setTimeout(() => res(3), 1300)
    ),
    Balle.one((res, rej) => 
        setTimeout(() => {
            try {
                throw 'Error occurred';
            } catch(e) { rej(e); }
        }, 200)
    )
])
.then(r => console.log('The result is', r))
.catch(err => console.log('The error is', err));