write-files
v1.0.0
Published
Write multiple files parallelly
Downloads
8
Maintainers
Readme
write-files
Write multiple files parallelly
const writeFiles = require('write-files');
writeFiles({
'foo.txt': 'foo',
'bar.txt': 'bar'
}, err => {
if (err) {
throw err;
}
fs.readFileSync('foo.txt', 'utf8'); //=> 'foo'
fs.readFileSync('bar.txt', 'utf8'); //=> 'bar'
});
Installation
npm install write-files
API
const writeFiles = require('write-files');
writeFiles(nameDataMap [, options], callback)
nameDataMap: Object
({"filename0": "contents0", "filename1": "contents1", ... }
)
options: Object
or String
(directly passed to write-file-atomic
)
callback: Function
The API is quite the same as write-file-atomic's. The only difference is write-files requires an Object
as its first argument: Each key of the object will be used as a file path, and each value of the object will be written to a path where the corresponding key indicates.
After one or more file output failed, it passes an error to its callback function and removes already created files.
const glob = require('glob');
const writeFiles = require('write-files');
writeFiles({
file1: '1', // success
file2: '2', // success
file3: '3', // fail
file4: '4' // success
}, err => {
!!err; //=> true
glob('file{1,2,3,4}' err, () => {
paths.length; //=> 0
});
});
License
Copyright (c) 2015 Shinnosuke Watanabe
Licensed under the MIT License.