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

deferred-fs

v0.3.2

Published

Provides a promise-based API for Node's filesystem APIs.

Downloads

15

Readme

deferred-fs

Provides a promise-based API for Node.js's filesystem API.

Requirements

Node.js

Installation

The easiest way is to install deferred-fs using npm, like this:

$ npm install deferred-fs

This will install deferred-fs into your project's node_modules subdirectory. From a Node app, you can then load dfs by doing require('deferred-fs').

Deferred-fs API

The object returned by require('deferred-fs') works exactly like Node's fs library, except that its asynchronous methods return a promise object instead of accepting a callback argument.

Here's an example showing how to use writeFile to asynchronously write a file and print a completion message when done:

var dfs = require('dfs');
dfs.writeFile('hello.txt', 'Hello world!', 'utf8').then(
    function () {
        console.log('File was written successfully.');
    });

In cases where a normal callback would return an "error" argument, the returned promise rejects, meaning the second callback is invoked:

var dfs = require('dfs');
dfs.readFile('/file_that_does_not_exist').then(
	null,
    function (error) {
        console.log('Yikes, an error occurred! ' + error);
    });

The above program will output:

Yikes, an error occurred! Error: ENOENT, open 'c:\file_that_does_not_exist'

The real advantage of the promise approach comes from chaining promises together, allowing you to transform values returned by earlier callbacks and implement high-level error handling.

Promise API

The promise object returned by deferred-fs's asynchronous methods has the familiar "then" API:

  • then(onResolve, onReject)

Internally these promises are implemented using the Deferred library from Eclipse Orion. This means they're 100% compliant with the Promises/A+ specification, version 1.1.

Deferred API

deferred-fs also exports Orion's Deferred library, giving you access to a richer API than simple promises:

var Deferred = require('deferred-fs').Deferred;

var promises = [ new Deferred().resolve('resolved!'), new Deferred().reject('rejected :(') ];
Deferred.all(promises).then(
  function(results) {
    console.log('All promises resolved: ' + results.join(', '));
  }, function(err) {
    console.log('An error occurred: ' + err);
  });

Consult the Deferred JSDoc for details of the available API methods. In addition to Promises/A+ compliance, Orion's Deferred also implements some evolving APIs like progress and cancellation.