How to use the close function from graceful-fs
Find comprehensive JavaScript graceful-fs.close code examples handpicked from public code repositorys.
graceful-fs.close is a function in Node.js that closes an open file descriptor.
GitHub: rstudio/shiny-server
98 99 100 101 102 103 104 105 106 107
} return str; }); }) .fin(function() { fs.close(fd, function(err) { if (err) logger.error("Couldn't close safeTail_p fd: " + err.message); }); });
GitHub: mmraff/untar-to-memory
100 101 102 103 104 105 106 107 108 109 110
} return params } const closeReadStream = rstr => { // fs.close(fileDescriptor) is nothing but a wild goose chase in this context. // Even when there's no error, it does not trigger a 'close' or 'end' event, // which we really need in order to know when to call the callback. // For compatibility with node engine v < 6, must resign to this way: rstr.resume()
+ 24 other calls in file
How does graceful-fs.close work?
graceful-fs.close is a function in Node.js that takes a file descriptor as its first argument, and a callback function as its second argument. When called, the function will attempt to close the file descriptor. If the file descriptor is not open or has already been closed, the function will immediately invoke the callback function without any errors. If an error occurs while attempting to close the file descriptor, the error will be passed as the first argument to the callback function. If the file descriptor is successfully closed, the callback function will be invoked with no arguments. The function can be used to free up resources associated with an open file descriptor when it is no longer needed. It can be particularly useful in scenarios where many file descriptors are opened and need to be closed in a timely and efficient manner.
GitHub: lesly-mlab/mlab-app-v2
27 28 29 30 31 32 33 34 35 36
if (err) return callback(err) fs.open(tmpfile, 'r+', (err, fd) => { if (err) return callback(err) fs.futimes(fd, d, d, err => { if (err) return callback(err) fs.close(fd, err => { if (err) return callback(err) fs.stat(tmpfile, (err, stats) => { if (err) return callback(err) callback(null, stats.mtime > 1435410243000)
13 14 15 16 17 18 19 20 21 22 23 24
function closeFd(propagatedErr, fd, callback) { if (typeof fd !== 'number') { return callback(propagatedErr); } fs.close(fd, onClosed); function onClosed(closeErr) { if (propagatedErr || closeErr) { return callback(propagatedErr || closeErr);
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12 13
const fs = require("graceful-fs"); const fd = fs.openSync("file.txt", "r"); // Do some work with the file descriptor... // Close the file descriptor when we are done with it. fs.close(fd, (err) => { if (err) { console.error(`Error closing file: ${err}`); } else { console.log(`File closed successfully.`); } });
In this example, we first use the fs.openSync method to open a file and obtain its file descriptor. We then perform some work with the file descriptor (not shown in this example), and when we are done, we pass the file descriptor and a callback function to the fs.close method. If an error occurs while attempting to close the file descriptor, the callback function will receive an error object as its first argument. If the file descriptor is successfully closed, the callback function will be invoked with no arguments. In this example, we simply log the success or failure of the file closure to the console.
121 122 123 124 125 126 127 128 129 130
}) }).then(function syncAndClose () { return new Promise(function (resolve, reject) { if (options.fsync !== false) { fs.fsync(fd, function (err) { if (err) fs.close(fd, () => reject(err)) else fs.close(fd, resolve) }) } else { fs.close(fd, resolve)
+ 15 other calls in file
GitHub: alanbabyjoseph/library
74 75 76 77 78 79 80 81 82 83
} function syncAndClose (err) { if (err) return cb(err) fs.fsync(fd, function (err) { if (err) return cb(err) fs.close(fd, cb) }) } }) }
+ 2 other calls in file
graceful-fs.promises is the most popular function in graceful-fs (1135 examples)