# deferred-fs

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

Latest version **0.3.2** (published 2014-03-04) · EPL+EDL license · 0 weekly downloads

## Install

```sh
npm install deferred-fs
pnpm add deferred-fs
yarn add deferred-fs
bun add deferred-fs
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.2 |
| Published | 2014-03-04 |
| First published | 2012-12-13 |
| Weekly downloads | 0 |
| License | EPL+EDL |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Orion contributors |
| Maintainers | mamacdon |
| Keywords | promise, deferred, node, fs, orion |

## Links

- npm: https://www.npmjs.com/package/deferred-fs
- Repository: https://github.com/mamacdon/deferred-fs
- Issues: https://github.com/mamacdon/deferred-fs/issues
- npm.io page: https://npm.io/package/deferred-fs

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 0.3.2 (latest) — 2014-03-04
- 0.3.1 — 2013-10-28
- 0.3.0 — 2013-10-09
- 0.2.3 — 2013-04-08
- 0.2.2 — 2013-02-12
- 0.2.1 — 2013-02-08
- 0.1.0 — 2013-01-22
- 0.0.3 — 2013-01-11
- 0.0.2 — 2013-01-04
- 0.0.1 — 2012-12-13

## README

# deferred-fs

Provides a promise-based API for Node.js's [filesystem API](http://nodejs.org/api/fs.html).

## 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:

```js
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:

```js
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](http://eclipse.org/orion). This means they're 100% compliant with the [Promises/A+ specification](https://github.com/promises-aplus/promises-spec), version 1.1.

## Deferred API
deferred-fs also exports Orion's Deferred library, giving you access to a richer API than simple promises:

```js
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](https://orionhub.org/jsdoc/symbols/orion.Deferred.html) for details of the available API methods. In addition to Promises/A+ compliance,
Orion's Deferred also implements some evolving APIs like [progress](https://github.com/promises-aplus/progress-spec) and 
[cancellation](https://github.com/promises-aplus/cancellation-spec).

---
_Source: https://npm.io/package/deferred-fs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
