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

ez-promise

v0.0.36

Published

An extremely lightweight way to promisify callback-based error-first functions

Downloads

61

Readme

EZ Promise

A lightweight way to convert error-first Node.js style callbacks into promises.

Simply take a function that follows the Node.js callback convention, pass it to ezpromise and pass any arguments afterwards.

ezpromise(rmdir, './path/to/dir')

Partial application / Currying / Thunks

Comes with built in support for currying, simply import ezThunk and you are ready to go. Pass the function you wish to convert into a promise first, with any arguments in the curried function.

import { ezThunk } from 'ez-promise'

const removeDir = ezThunk(rmdir)

removeDir('./path/to/dir').then(() => { console.log('Success!') })

Batteries Included

The library comes with everything required to run. If no window.Promise is detected, a polyfill is automatically added.

import/require

const ezpromise = require('ez-promise').default
import ezpromise from 'ez-promise'

const { ezThunk } = require('ez-promise')
import { ezThunk } from 'ez-promise'

Resolving Multiple Values

Since a promise can only resolve a single value, if a function's callback has an arity of 3 or greater an array containing the values is returned.

ezpromise(someAsyncFn, './some/path')
    .then(([arg1, arg2, ...rest]) => {
        // ...
    })

Example

We have an environment script that takes an environment config object from a set of available configs and dumps it into an environment folder inside our source code.

Libraries like fs, ncp, and rmdir all make use of async callbacks, and since they follow the error-first callback convention (AKA Node.js-style callbacks), we can easily convert them into promises.

Let's start with the callback pattern:

import ncp from 'ncp'

function copyFile (from, to) {
    ncp(from, to, err => {
        if (err) {
            console.log(`Error copying file: ${err}`)
        } else {
            console.log('Successfully copied file!')
        }
    )
}

copyFile('./env/production.js', './src/env.js')

Now, let's convert our copyFile function into a promise:

import ncp from 'ncp'
import ezpromise from 'ez-promise'

const copyFile = ({ from, to }) => ezpromise(ncp, from, to)

Or if you prefer to use the curried version:

import ncp from 'ncp'
import { ezThunk } from 'ez-promise'

const copyFile = ezThunk(ncp)

That's it! Now we are free to use our newly-converted promises anyway we like.

Chaining promise methods:

copyFile({ from: './env/production.js', to: './src/env.js' })
    .then(() => { console.log('Success!') })
    .catch(err => { console.log(`Error: ${err}`) })

Async/await:

async function multipleTasks () {
    const result = await copyFile({ to: './env/production.js', to: './src/env.js' })
    // ...
}

Note that when currying, the arguments will be in the original format of the function we are converting into a promise, but with the callback at the end omitted:

copyFile('./env/production.js', './src/env.js')
ncp('./env/production.js', './src/env.js', () => {
    // ...
})

Dependencies

The only dependency is a Promise polyfill!