fs-readfile-promise
Version:
Promise version of fs.readFile
55 lines (37 loc) • 2.08 kB
Markdown
//img.shields.io/npm/v/fs-readfile-promise.svg)](https://www.npmjs.com/package/fs-readfile-promise)
[](https://travis-ci.org/shinnn/fs-readfile-promise)
[](https://ci.appveyor.com/project/ShinnosukeWatanabe/fs-readfile-promise)
[](https://coveralls.io/r/shinnn/fs-readfile-promise)
Promisified version of [`fs.readFile`][fs.readfile]
```javascript
const readFilePromise = require('fs-readfile-promise');
(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'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).
[ ](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/getting-started/what-is-npm).
```
npm install fs-readfile-promise
```
```javascript
const readFilePromise = require('fs-readfile-promise');
```
*path*: `string` `Buffer` `URL` `integer`
*options*: `Object` ([fs.readFile] options) or `string` (encoding)
Return: `Promise<Buffer|string>`
```javascript
(async () => {
await readFilePromise('src.txt'); //=> <Buffer 48 69 2e>
await readFilePromise('src.txt', 'utf8'); //=> 'Hi.'
await readFilePromise('src.txt', {encoding: 'base64'}); //=> 'SGku'
})();
```
[ ](./LICENSE) © 2017 Shinnosuke Watanabe
[ ]: https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback
[![npm version](https: