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

neat-callbacks

v1.2.1

Published

A nifty little package that prevents callback hell (hopefully)

Downloads

14

Readme

neat callbacks

Before:

function before() {
    console.log("A,");

    setTimeout(() => {
        console.log("B,");
    }, 1000);

    console.log("C.");
}

before();
// would output:
// A, C. B,

With neat-callbacks:

const after = NeatFunction.from(function* () {
    console.log("A,");

    yield Timeout(1000);

    console.log("B,");
    console.log("C.");

})

after();
// would output:
// A, B, C.

You have to imagine yield like a await keyword, just outside of an async function.

For exemple:

// The default way to do it:

const request = new Promise((url) => {
    fetch(url).then((data) => {
        data.text.then((text) => {
            resolve(text);
        });
    });
});

var text;

request("./text.txt").then(data => {
    text = data;
});


// With neat-callbacks it is much cleaner; 

var text;

const request = (function* (url) {
    text = yield (yield fetch(url)).text;
}).toNeatFunction();

request("./text.txt");

// Or using XMLHttpRequest

const request = NeatFunction.from(function* (url, object, method = "get") {
    var req = new XMLHttpRequest();

    yield (resolve) => {
        req.open(method, url, true);
        req.send();

        req.onload = () => {
            resolve(req.responseText);
        };
    };

    object = req;
});

Installing

npm i neat-callbacks

then, import it with require,

const { Timeout, NeatFunction } = require("neat-callbacks");

Or with

<script src="./node_modules/neat-callbacks/browser.js"></script>

Usage

NeatFunction.from

To create a neatFunction, you can use the function NeatFunction.from and passing a generator function as the first argument.

Please keep in mind that neat functions aren't async function, since they can't return any data.

Exemple:

NeatFunction.from(function* () {
    // code
});

Timeout

As you might have noticed earlier, I used a helper function called Timeout.

Timeout pauses the function for a set amount of milliseconds.

Exemple:

const timer = NeatFunction.from(function* (Seconds = 0) {
    console.log("Timer started");
    yield Timeout(Seconds * 1000);
    console.log("Time's up!");
});

timer(3);

NeatFunction constructor

There is also a NeatFunction constructor which allow to create a neat function the same way you would with the function constructor:

Exemples

// the last argument is always code, and the rest are arguments
new NeatFunction("a", "b",
    "yield Timeout(1000);"+
    "console.log(a+b);")
    (1,2);

On node, if you want to run a file that uses yield in the global scope you use it like so:

new NeatFunction(fs.readFileSync("./test.js", "utf8"))();

What is yield-able

  • promises
  • async functions
  • functions, the function will be called with the two first argument being resolve and reject

yield*

You can use the built-in yield* keyword to use a iteratable.

NeatFunction.from(function* (callback) {
    yield* [
        promise1,
        promise2,
        promise3
    ]

    // all three promises executed one after the other.

})

Or even

NeatFunction.from(function* (callback) {
    yield* (function* {
        for (let i = 0; i < 10; i++) {
            yield i;
        }
    });
})

To receive data from the neat function:

const request = NeatFunction.from(function* (callback) {

    // fetch your data

    callback(data);

})

request(data => {
    // do something with it
});

After developing this package I realised that something quite similar had already been done, check out co if this package doesn't have enough features for you.

Contributing

If you wish to contribute to this project, you can head over the github page and create a pull request. I'm not super active on github and npm, so don't panic if your pull request isn't seen

License

MIT