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: undocumented magic: async iterator over fs.Dir will call dir.close() on iteration end #35148

Closed
1 task
ZYinMD opened this issue Sep 11, 2020 · 0 comments
Closed
1 task
Labels
doc Issues and PRs related to the documentations.

Comments

@ZYinMD
Copy link

ZYinMD commented Sep 11, 2020

📗 API Reference Docs Problem

  • Version: ✍️v14.10.1
  • Subsystem: ✍️
    fs

Location

Section of the site where the content exists

Affected URL(s):

https://nodejs.org/api/fs.html#fs_class_fs_dir

Description

I'm trying the fs.Dir class which was added since node v12.12:

// hello-world.js
const fs = require('fs');

(async function () {
  const dir = await fs.promises.opendir(__dirname);
  for await (const dirent of dir) {
    console.log('name:', dirent.name, 'isDir:', dirent.isDirectory());
  }
  return dir.close();
})();
$ node hello-world.js

It works as expected, but in the end when calling dir.close(), it throws (node:3218) UnhandledPromiseRejectionWarning: Error [ERR_DIR_CLOSED]: Directory handle was closed at Dir.close (internal/fs/dir.js:161:13).

After asking on stackoverflow, with the help of others, I found that the async iterator will auto close the dir after iteration is over, so I don't need to (and cannot) clean up with dir.close() myself. This can be seen in the source code here.

This magic is probably good for many developers, but it also throws for those who are careful to code correctly, so I think it should be mentioned in the doc.


  • I would like to work on this issue and
    submit a pull request.
@ZYinMD ZYinMD added the doc Issues and PRs related to the documentations. label Sep 11, 2020
@ZYinMD ZYinMD changed the title doc: undocumented magic: async iterator over fs.opendir() will call dir.close() on iteration end doc: undocumented magic: async iterator over fs.Dir will call dir.close() on iteration end Sep 11, 2020
jasnell added a commit to jasnell/node that referenced this issue Apr 27, 2021
targos pushed a commit that referenced this issue Apr 29, 2021
Signed-off-by: James M Snell <[email protected]>
Fixes: #35148

PR-URL: #38438
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
targos pushed a commit that referenced this issue Sep 4, 2021
Signed-off-by: James M Snell <[email protected]>
Fixes: #35148

PR-URL: #38438
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant