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

fs: use a default callback for fs.close() #37174

Closed
wants to merge 3 commits into from
Closed
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
8 changes: 7 additions & 1 deletion doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1617,10 +1617,13 @@ This is the synchronous version of [`fs.chown()`][].

See also: chown(2).

## `fs.close(fd, callback)`
## `fs.close(fd[, callback])`
<!-- YAML
added: v0.0.2
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/37174
description: A default callback is now used if one is not provided.
- version: v10.0.0
pr-url: https://github.com/nodejs/node/pull/12562
description: The `callback` parameter is no longer optional. Not passing
Expand All @@ -1641,6 +1644,9 @@ to the completion callback.
Calling `fs.close()` on any file descriptor (`fd`) that is currently in use
through any other `fs` operation may lead to undefined behavior.

If the `callback` argument is omitted, a default callback function that rethrows
any error as an uncaught exception will be used.

## `fs.closeSync(fd)`
<!-- YAML
added: v0.1.21
Expand Down
9 changes: 7 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,9 +438,14 @@ function readFileSync(path, options) {
return buffer;
}

function close(fd, callback) {
function defaultCloseCallback(err) {
if (err != null) throw err;
}

function close(fd, callback = defaultCloseCallback) {
validateInt32(fd, 'fd', 0);
callback = makeCallback(callback);
if (callback !== defaultCloseCallback)
callback = makeCallback(callback);

const req = new FSReqCallback();
req.oncomplete = callback;
Expand Down