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

fs.promised

v3.0.0

Published

Promise-implementation-agnostic wrapper for fs

Downloads

9,325

Readme

Promise-implementation-agnostic wrapper for fs

Promises are great. Node's default implementation of the fs module, using callbacks, are not. What's also not great is a fs wrapper that is dependant upon a specific promise implementation, especially since native promises have landed in ES2015. This library intends to be a drop-in replacement for the built-in fs module, where all the async functions now use Promises instead of callbacks. Any other functions on the fs module are passed through untouched.

Notes

  • All the examples in this README will be using ES2015/ES2016. The library is written in ES3 for maximum compatibility, but Promises are just so much easier to work with in ES2015+.

  • By default, this library will use global.Promise as its promise implementation. If you are using an older version of node, or want to use a different Promise implementation for any reason, you need to do something like this:

    var fs = require("fs.promised/promisify")(require("bluebird"));

    The promisify include takes a second optional parameter, shouldCache, which will cache the generated promise wrappers so that when promisify is called in another module with the same Promise implementation the same wrapper is returned. Set this to false if you wish to disable.

Usage

const fs = require("fs.promised");

const doSomething_ES2015 = function () {
    return fs.mkdir("/tmp/fs")
    .then(() => fs.writeFile("/tmp/fs/test", "blah blah blah"))
    .then(() => fs.unlink("/tmp/fs/test"))
    ;
}

const doSomething_ES2016 = async function () {
    await fs.mkdir("/tmp/fs");
    await fs.writeFile("/tmp/fs/test", "blah blah blah");
    await fs.unlink("/tmp/fs/test");
}

Caveats

  • Exceptions: If the wrapped function throws an error, the promise will be rejected with the the erro. This shouldn't matter as async fs functions shouldn't throw errors synchonously but the promise will still capture it if it does.

  • Callbacks with multiple success values: This should only affect fs.write and fs.read. Functions that give a callback more than one success value (as in, values after the first "error" value) will be resolved with an array. Example:

    const multiArgs_ES2015 = function () {
        return fs.read(fd, data)
        .then(([ written, string ]) => {
            //...
        })
        ;
    };
    
    /* ES2016 */
    const multiArgs_ES2016 = async function () {
        let [written, string] = await fs.read(fd, data);
        //...
    };