Skip to content

Commit 1613f20

Browse files
committed
Update docs for promise support
1 parent d41ed1e commit 1613f20

File tree

12 files changed

+130
-12
lines changed

12 files changed

+130
-12
lines changed

docs/copy.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# copy(src, dest, [options], callback)
1+
# copy(src, dest, [options, callback])
22

33
Copy a file or directory. The directory can have contents. Like `cp -r`.
44

@@ -28,6 +28,15 @@ fs.copy('/tmp/mydir', '/tmp/mynewdir', err => {
2828

2929
console.log('success!')
3030
}) // copies directory, even if it has subdirectories or files
31+
32+
// Promise usage:
33+
fs.copy('/tmp/myfile', '/tmp/mynewfile')
34+
.then(() => {
35+
console.log('success!')
36+
})
37+
.catch(err => {
38+
// handle error
39+
})
3140
```
3241

3342
**Using filter function**

docs/emptyDir.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,13 @@ fs.emptyDir('/tmp/some/dir', err => {
1818

1919
console.log('success!')
2020
})
21+
22+
// With promises
23+
fs.emptyDir('/tmp/some/dir')
24+
.then(() => {
25+
console.log('success!')
26+
})
27+
.catch(err => {
28+
// handle error
29+
})
2130
```

docs/ensureDir.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ensureDir(dir, callback)
1+
# ensureDir(dir, [callback])
22

33
Ensures that the directory exists. If the directory structure does not exist, it is created. Like `mkdir -p`.
44

@@ -17,4 +17,13 @@ fs.ensureDir(dir, err => {
1717
console.log(err) // => null
1818
// dir has now been created, including the directory it is to be placed in
1919
})
20+
21+
// With Promises:
22+
fs.ensureDir(dir)
23+
.then(() => {
24+
console.log('success!')
25+
})
26+
.catch(err => {
27+
// handle error
28+
})
2029
```

docs/ensureFile.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ensureFile(file, callback)
1+
# ensureFile(file, [callback])
22

33
Ensures that the file exists. If the file that is requested to be created is in directories that do not exist, these directories are created. If the file already exists, it is **NOT MODIFIED**.
44

@@ -17,4 +17,13 @@ fs.ensureFile(file, err => {
1717
console.log(err) // => null
1818
// file has now been created, including the directory it is to be placed in
1919
})
20+
21+
// With Promises:
22+
fs.ensureFile(file)
23+
.then(() => {
24+
console.log('success!')
25+
})
26+
.catch(err => {
27+
// handle error
28+
})
2029
```

docs/ensureLink.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ensureLink(srcpath, dstpath, callback)
1+
# ensureLink(srcpath, dstpath, [callback])
22

33
Ensures that the link exists. If the directory structure does not exist, it is created.
44

@@ -17,4 +17,13 @@ fs.ensureLink(srcpath, dstpath, err => {
1717
console.log(err) // => null
1818
// link has now been created, including the directory it is to be placed in
1919
})
20+
21+
// With Promises:
22+
fs.ensureLink(srcpath, dstpath)
23+
.then(() => {
24+
console.log('success!')
25+
})
26+
.catch(err => {
27+
// handle error
28+
})
2029
```

docs/ensureSymlink.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ensureSymlink(srcpath, dstpath, [type], callback)
1+
# ensureSymlink(srcpath, dstpath, [type, callback])
22

33
Ensures that the symlink exists. If the directory structure does not exist, it is created.
44

@@ -18,4 +18,13 @@ fs.ensureSymlink(srcpath, dstpath, err => {
1818
console.log(err) // => null
1919
// symlink has now been created, including the directory it is to be placed in
2020
})
21+
22+
// With Promises:
23+
fs.ensureSymlink(srcpath, dstpath)
24+
.then(() => {
25+
console.log('success!')
26+
})
27+
.catch(err => {
28+
// handle error
29+
})
2130
```

docs/move.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# move(src, dest, [options], callback)
1+
# move(src, dest, [options, callback])
22

33
Moves a file or directory, even across devices.
44

@@ -18,6 +18,14 @@ fs.move('/tmp/somefile', '/tmp/does/not/exist/yet/somefile', err => {
1818

1919
console.log('success!')
2020
})
21+
22+
fs.move('/tmp/somefile', '/tmp/does/not/exist/yet/somefile')
23+
.then(() => {
24+
console.log('success!')
25+
})
26+
.catch(err => {
27+
// handle error
28+
})
2129
```
2230

2331
**Using `overwrite` option**

docs/outputFile.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# outputFile(file, data, [options], callback)
1+
# outputFile(file, data, [options, callback])
22

33
Almost the same as `writeFile` (i.e. it [overwrites](http://pages.citebite.com/v2o5n8l2f5reb)), except that if the parent directory does not exist, it's created. `file` must be a file path (a buffer or a file descriptor is not allowed). `options` are what you'd pass to [`fs.writeFile()`](https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback).
44

@@ -20,4 +20,14 @@ fs.outputFile(file, 'hello!', err => {
2020
console.log(data) // => hello!
2121
})
2222
})
23+
24+
// With Promises:
25+
fs.outputFile(file, 'hello!')
26+
.then(() => fs.readFile(file, 'utf8'))
27+
.then(data => {
28+
console.log(data) // => hello!
29+
})
30+
.catch(err => {
31+
// handle error
32+
})
2333
```

docs/outputJson.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# outputJson(file, object, [options], callback)
1+
# outputJson(file, object, [options, callback])
22

33
Almost the same as [`writeJson`](writeJson.md), except that if the directory does not exist, it's created.
44
`options` are what you'd pass to [`jsonFile.writeFile()`](https://github.com/jprichardson/node-jsonfile#writefilefilename-options-callback).
@@ -23,4 +23,14 @@ fs.outputJson(file, {name: 'JP'}, err => {
2323
console.log(data.name) // => JP
2424
})
2525
})
26+
27+
// With Promises:
28+
fs.outputJson(file, {name: 'JP'})
29+
.then(() => fs.readJson(file))
30+
.then(data => {
31+
console.log(data.name) // => JP
32+
})
33+
.catch(err => {
34+
// handle error
35+
})
2636
```

docs/readJson.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# readJson(file, [options], callback)
1+
# readJson(file, [options, callback])
22

33
Reads a JSON file and then parses it into an object. `options` are the same
44
that you'd pass to [`jsonFile.readFile`](https://github.com/jprichardson/node-jsonfile#readfilefilename-options-callback).
@@ -16,9 +16,18 @@ const fs = require('fs-extra')
1616

1717
fs.readJson('./package.json', (err, packageObj) => {
1818
if (err) console.error(err)
19-
19+
2020
console.log(packageObj.version) // => 0.1.3
2121
})
22+
23+
// Promise Usage
24+
fs.readJson('./package.json')
25+
.then(packageObj => {
26+
console.log(packageObj.version) // => 0.1.3
27+
})
28+
.catch(err => {
29+
// handle error
30+
})
2231
```
2332

2433
---
@@ -37,4 +46,13 @@ fs.readJson(file, { throws: false }, (err, obj) => {
3746

3847
console.log(obj) // => null
3948
})
49+
50+
// Promise Usage
51+
fs.readJson(file, { throws: false })
52+
.then(obj => {
53+
console.log(obj) // => null
54+
})
55+
.catch(err => {
56+
// Not called
57+
})
4058
```

0 commit comments

Comments
 (0)