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

thunkifiers

v0.1.0

Published

Thunkifiers for various callback styles.

Downloads

2

Readme

thunkifiers

A set of thunkifiers which transform functions using various common callback styles into thunks, for use in generators or other control flow mechanisms.

Based on thunkify by visionmedia, from which it inherits some code.

What is a thunk?

A thunk is a wrapper around a function that expects or returns one or more callbacks.

It returns another function which expects a normalized node-style callback, partially applied with the provided arguments.

function original(arguments, done) {
    // ...
    done(null, 'ok')
}

function thunkified(arguments) {
    return function(done) {
        original(arguments, done)
    }
}

Thunks catch any exception thrown during the execution of both their wrapped function and the callback, and expose it as the err argument of the callback.

Callbacks will be debounced to prevent additional "ghost calls". Note that this won't hold if errors are thrown during the callback itself, but you aren't really expected to be using thunks by themselves.

Thunks are a lightweight implementation of Futures which do not provide any mechanisms of control flow. As such, they are meant to be fed into generators or other systems which provide their own mechanisms.

For a more in-depth explanation and a comparison with Promises read thunks vs promises.

Installation

$ npm install thunkifiers

Included thunkifiers

All thunkifiers expose the same interface:

function thunkifier (
    function,
    [object context]
)

The first argument being the function to thunkify, and optionally a context object to bind the wrapped function to, which defaults to the receiver on which the thunk is called.

They all return a thunk, which accepts any arguments and returns a partially applied lazy invocation.

thunk(arguments...)
// => function(done)

Node-style error-first

function thunkifiers.node (
    function (args..., done),
    [object context]
)

A thunkifier for functions that accept a single (err, arguments...) callback. This includes most Node.js functions.

Wrapped functions will maintain their receiver as this, but you can optionally provide a specific context to bind them on.

function read(filename, encoding, done) {
    // ...
    if (results)
        done(null, results)
    else
        done('error')
}

var thunkifiers = require('thunkifiers'),
    read = thunkifiers.node(read)

read('package.json', 'utf8')(function(err, str){
    // ...
})

Branched callbacks for success and error

function thunkifiers.branched (
    function (args..., success, error),
    [object context]
)

A thunkifier for functions that accept a (arguments...) success callback and a (error, error arguments...) error callback.

Wrapped functions will maintain their receiver as this, but you can optionally provide a specific context to bind them on.

function read(filename, encoding, success, error) {
    // ...
    if (results)
        success(results)
    else
        error('error')
}

var thunkifiers = require('thunkifiers'),
    read = thunkifiers.branched(read)

read('package.json', 'utf8')(function(err, str){
    // ...
})

Promises

There isn't a thunkifier for promises yet. Pull requests are welcome.

License

The MIT License (MIT)

Copyright (c) 2014, zenoamaro at gmail dot com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.