Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions lib/node_modules/@stdlib/fs/read-dir/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ function onRead( error, data ) {
Synchronously reads the contents of a directory.

```javascript
var isError = require( '@stdlib/assert/is-error' );
Comment thread
kgryte marked this conversation as resolved.

var out = readDir.sync( __dirname );
if ( out instanceof Error ) {
if ( isError( out ) ) {
throw out;
}
console.log( out );
Expand All @@ -70,6 +72,8 @@ console.log( out );

- The difference between this module and [`fs.readdirSync()`][fs] is that [`fs.readdirSync()`][fs] will throw if an `error` is encountered (e.g., if given a non-existent `path`) and this module will return an `error`. Hence, the following anti-pattern

<!-- eslint-disable no-restricted-syntax, n/no-sync -->

```javascript
var fs = require( 'fs' );

Expand All @@ -81,16 +85,19 @@ console.log( out );
}
```

<!-- eslint-enable no-restricted-syntax, n/no-sync -->

can be replaced by an approach which addresses existence via `error` handling.

```javascript
var isError = require( '@stdlib/assert/is-error' );
var readDir = require( '@stdlib/fs/read-dir' );

var dir = '/path/to/dir';

// Explicitly handle the error...
dir = readDir.sync( dir );
if ( dir instanceof Error ) {
if ( isError( dir ) ) {
// You choose what to do...
throw dir;
}
Expand All @@ -107,20 +114,21 @@ console.log( out );
<!-- eslint no-undef: "error" -->

```javascript
var isError = require( '@stdlib/assert/is-error' );
var readDir = require( '@stdlib/fs/read-dir' );

/* Sync */

var out = readDir.sync( __dirname );
// returns <Array>

console.log( out instanceof Error );
console.log( isError( out ) );
// => false

out = readDir.sync( 'beepboop' );
// returns <Error>

console.log( out instanceof Error );
console.log( isError( out ) );
// => true

/* Async */
Expand Down
5 changes: 3 additions & 2 deletions lib/node_modules/@stdlib/fs/read-dir/examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@

'use strict';

var isError = require( '@stdlib/assert/is-error' );
var readDir = require( './../lib' );

/* Sync */

var out = readDir.sync( __dirname );
// returns <Array>

console.log( out instanceof Error );
console.log( isError( out ) );
// => false

out = readDir.sync( 'beepboop' );
// returns <Error>

console.log( out instanceof Error );
console.log( isError( out ) );
// => true

/* Async */
Expand Down
Loading