fs-readfile-promise
Advanced tools
Comparing version 3.0.0 to 3.0.1
20
index.js
'use strict'; | ||
const fs = require('graceful-fs'); | ||
const readFile = require('graceful-fs').readFile; | ||
module.exports = function fsReadFilePromise(filePath, options) { | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(filePath, options, (err, data) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
return new Promise((resolve, reject) => { | ||
readFile(filePath, options, (err, data) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(data); | ||
}); | ||
}); | ||
resolve(data); | ||
}); | ||
}); | ||
}; |
{ | ||
"name": "fs-readfile-promise", | ||
"version": "3.0.0", | ||
"description": "Promise version of fs.readFile", | ||
"repository": "shinnn/fs-readfile-promise", | ||
"author": "Shinnosuke Watanabe (https://github.com/shinnn)", | ||
"scripts": { | ||
"pretest": "eslint --fix --config @shinnn/node index.js test.js", | ||
"test": "node --strong_mode test.js", | ||
"coverage": "node --strong_mode node_modules/.bin/istanbul cover test.js" | ||
}, | ||
"license": "MIT", | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"fs", | ||
"read", | ||
"file", | ||
"promise", | ||
"promises", | ||
"then", | ||
"thenable" | ||
], | ||
"dependencies": { | ||
"graceful-fs": "^4.1.2" | ||
}, | ||
"devDependencies": { | ||
"@shinnn/eslint-config-node": "^2.0.0", | ||
"eslint": "^2.10.0", | ||
"istanbul": "^0.4.3", | ||
"tape": "^4.5.1" | ||
} | ||
"name": "fs-readfile-promise", | ||
"version": "3.0.1", | ||
"description": "Promise version of fs.readFile", | ||
"repository": "shinnn/fs-readfile-promise", | ||
"author": "Shinnosuke Watanabe (https://github.com/shinnn)", | ||
"scripts": { | ||
"pretest": "eslint --fix --format=codeframe index.js test.js", | ||
"test": "nyc node test.js" | ||
}, | ||
"license": "ISC", | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"fs", | ||
"read", | ||
"file", | ||
"promise", | ||
"promises", | ||
"then", | ||
"thenable" | ||
], | ||
"dependencies": { | ||
"graceful-fs": "^4.1.11" | ||
}, | ||
"devDependencies": { | ||
"@shinnn/eslint-config-node": "^5.0.0", | ||
"eslint": "^4.14.0", | ||
"nyc": "^11.4.1", | ||
"tape": "^4.8.0" | ||
}, | ||
"eslintConfig": { | ||
"extends": "@shinnn/node" | ||
} | ||
} |
@@ -1,25 +0,24 @@ | ||
# fs-readfile-promise | ||
# fs-readfile-promise | ||
[![NPM version](https://img.shields.io/npm/v/fs-readfile-promise.svg)](https://www.npmjs.com/package/fs-readfile-promise) | ||
[![npm version](https://img.shields.io/npm/v/fs-readfile-promise.svg)](https://www.npmjs.com/package/fs-readfile-promise) | ||
[![Build Status](https://travis-ci.org/shinnn/fs-readfile-promise.svg?branch=master)](https://travis-ci.org/shinnn/fs-readfile-promise) | ||
[![Build status](https://ci.appveyor.com/api/projects/status/5sacvq0w9x7mwkwd?svg=true)](https://ci.appveyor.com/project/ShinnosukeWatanabe/fs-readfile-promise) | ||
[![Coverage Status](https://img.shields.io/coveralls/shinnn/fs-readfile-promise.svg)](https://coveralls.io/r/shinnn/fs-readfile-promise) | ||
[![Dependency Status](https://david-dm.org/shinnn/fs-readfile-promise.svg)](https://david-dm.org/shinnn/fs-readfile-promise) | ||
[![devDependency Status](https://david-dm.org/shinnn/fs-readfile-promise/dev-status.svg)](https://david-dm.org/shinnn/fs-readfile-promise#info=devDependencies) | ||
[Promises/A+][promise] version of [`fs.readFile`][fs.readfile] | ||
Promisified version of [`fs.readFile`][fs.readfile] | ||
```javascript | ||
const readFile = require('fs-readfile-promise'); | ||
const readFilePromise = require('fs-readfile-promise'); | ||
readFile('path/to/file') | ||
.then(buffer => console.log(buffer.toString())) | ||
.catch(err => console.error(err.message)); | ||
(async () => { | ||
const buffer = await readFile('path/to/a/file'); | ||
buffer.toString(); //=> '... file contents ...' | ||
})(); | ||
``` | ||
Based on the principle of [*modular programming*](https://en.wikipedia.org/wiki/Modular_programming), this module has only one functionality [`readFile`][fs.readfile], unlike other promise-based file system modules. If you want to use a bunch of other [`fs`](http://nodejs.org/api/fs.html) methods in the promises' way, choose other modules such as [q-io](https://github.com/kriskowal/q-io) and [fs-promise](https://github.com/kevinbeaty/fs-promise). | ||
Based on the principle of [*modular programming*](https://en.wikipedia.org/wiki/Modular_programming), this module has only one functionality [`readFile`][fs.readfile], unlike other Promise-based file system modules. If you'd like to use a bunch of other [`fs`](http://nodejs.org/api/fs.html) methods in the Promise way, choose other modules such as [q-io](https://github.com/kriskowal/q-io) and [promise-fs](https://github.com/octet-stream/promise-fs). | ||
## Installation | ||
[Use npm.](https://docs.npmjs.com/cli/install) | ||
[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/getting-started/what-is-npm). | ||
@@ -33,22 +32,18 @@ ``` | ||
```javascript | ||
const readFile = require('fs-readfile-promise'); | ||
const readFilePromise = require('fs-readfile-promise'); | ||
``` | ||
### readFile(*filename* [, *options*]) | ||
### readFile(*path* [, *options*]) | ||
*filename*: `String` | ||
*options*: `Object` or `String` ([fs.readFile] options) | ||
Return: `Object` ([Promise][promise]) | ||
*path*: `string` `Buffer` `URL` `integer` | ||
*options*: `Object` ([fs.readFile] options) or `string` (encoding) | ||
Return: `Promise<Buffer|string>` | ||
When it finish reading the file, it will be [*fulfilled*](https://promisesaplus.com/#point-26) with an [`Buffer`](https://nodejs.org/api/buffer.html#buffer_buffer) of the file as its first argument. | ||
When it fails to read the file, it will be [*rejected*](https://promisesaplus.com/#point-30) with an error as its first argument. | ||
```javascript | ||
const readFile = require('fs-readfile-promise'); | ||
(async () => { | ||
await readFilePromise('src.txt'); //=> <Buffer 48 69 2e> | ||
await readFilePromise('src.txt', 'utf8'); //=> 'Hi.' | ||
await readFilePromise('src.txt', {encoding: 'base64'}); //=> 'SGku' | ||
})(); | ||
const onFulfilled = buffer => console.log(buffer.toString()); | ||
const onRejected = err => console.log('Cannot read the file.'); | ||
readFile('path/to/file').then(onFulfilled, onRejected); | ||
``` | ||
@@ -58,7 +53,4 @@ | ||
Copyright (c) 2014 - 2016 [Shinnosuke Watanabe](https://github.com/shinnn) | ||
[ISC License](./LICENSE) © 2017 Shinnosuke Watanabe | ||
Licensed under [the MIT License](./LICENSE). | ||
[fs.readfile]: https://nodejs.org/api/fs.html#fs_fs_readfile_filename_options_callback | ||
[promise]: https://promisesaplus.com/ | ||
[fs.readfile]: https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3839
55
Updatedgraceful-fs@^4.1.11