Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: add fspromises mkdir example #40843

Merged
merged 8 commits into from
Jun 12, 2022
Merged
33 changes: 33 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,39 @@ property indicating whether parent directories should be created. Calling
`fsPromises.mkdir()` when `path` is a directory that exists results in a
rejection only when `recursive` is false.

```mjs
import { mkdir } from 'fs/promises';

try {
const path = new URL('./test/project', import.meta.url);
const createDir = await mkdir(path, { recursive: true });

console.log(`created ${createDir}`);
} catch (err) {
console.error(err.message);
}
```

```cjs
const { mkdir } = require('fs/promises');
const { resolve } = require('path');

async function makeDirectory() {
try {
const path = resolve('./test/project/lol/hi');
const dirCreation = await mkdir(path, { recursive: true });

console.log(dirCreation);
return dirCreation;
} catch (err) {
console.error(err.message);
}

}

makeDirectory();
Copy link
Contributor

@aduh95 aduh95 Jun 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: instead of a try/catch, I would put a .catch(console.error): calling an async function without awaiting it or having a catch handler should be frowned upon imho.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you want me to move the MJS example to be a function and do the same, or should that continue using top-level await in a try/catch?

```

### `fsPromises.mkdtemp(prefix[, options])`

<!-- YAML
Expand Down