-
-
Notifications
You must be signed in to change notification settings - Fork 34.8k
doc: fix filehandle.truncate sample codes #20913
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3572,8 +3572,15 @@ console.log(fs.readFileSync('temp.txt', 'utf8')); | |
| // Prints: Node.js | ||
|
|
||
| async function doTruncate() { | ||
| const fd = await fsPromises.open('temp.txt', 'r+'); | ||
| await fsPromises.ftruncate(fd, 4); | ||
| let filehandle; | ||
| try { | ||
| filehandle = await fsPromises.open('temp.txt', 'r+'); | ||
| await filehandle.truncate(4); | ||
| } finally { | ||
| if (filehandle !== null) { | ||
| await filehandle.close(); | ||
| } | ||
| } | ||
| console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints: Node | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these should really be.... let filehandle;
try {
const filehandle = await fsPromises.open('temp.txt', 'r+');
await filehandle.truncate(4);
} finally {
await filehandle.close();
}While the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jasnell sorry for the tangent: we really need a better resources story in general - if this is tricky consider how hard this is with 3 concurrent handles.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One thing that might work here API wise is a disposer: await fsPromises.open('temp.txt', 'r+', async handle => {
await handle.truncate(4);
});This doesn't solve the harder problem of dealing with multiple resources at once though.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I certainly don't disagree :-)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jasnell
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jasnell - what do you think the right avenue to discuss such a cross-cutting concern is? I'm not sure what the right team/working-group/avenue is. I'm a little lost here :) |
||
| } | ||
|
|
||
|
|
@@ -3591,8 +3598,15 @@ console.log(fs.readFileSync('temp.txt', 'utf8')); | |
| // Prints: Node.js | ||
|
|
||
| async function doTruncate() { | ||
| const fd = await fsPromises.open('temp.txt', 'r+'); | ||
| await fsPromises.ftruncate(fd, 10); | ||
| let filehandle; | ||
| try { | ||
| filehandle = await fsPromises.open('temp.txt', 'r+'); | ||
| await filehandle.truncate(10); | ||
| } finally { | ||
| if (filehandle !== null) { | ||
| await filehandle.close(); | ||
| } | ||
| } | ||
| console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints Node.js\0\0\0 | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What can make undefined
filehandlebenull?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
openthrowing, since that was unclear it'll definitely be unclear for some users - we should probably add a comment.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
openthrows, will not thefilehandleremainundefined?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could have sworn that was a
!=and not a!==, good spotting!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, shoud not we initialize
filehandleasnullor use non-strict comparison or evenif (filehandle)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should. I was saying "good spotting" for spotting that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, GitHub did not update timely with new comments)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vsemozhetbyt @benjamingr
filehandleis notundefined. It is defined and initialized asnull.But it is unclear for some users.
The below code may be more clearly for users. It looks better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you do that in JavaScript that initializes the value to
undefined. You can verify this in the following script:Yes, thank you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@benjamingr
oh, sorry! I was wrong and fixed them.