-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
Add variant of fs.create{Read,Write}Stream
to fs/promises
#21342
Comments
To clean up the resource, you should use finally, which can also address the problem when you throw an error. |
@simonkcleung This isn't about cleanup. It's about errors during initialization. |
I would prefer this to be a chance to change the readable/writeable streams returned by fs module. E.g. something like a |
@ChALkeR I would be mildly okay with that, too, but I'd still like the Using this + your addition would convert this: // Old with callbacks
const input = fs.createWriteStream(infile)
const output = fs.createWriteStream(outfile)
let readyIn = false
let readyOut = false
input.on("error", error)
output.on("error", error)
input.on("ready", onReadyIn)
output.on("ready", onReadyOut)
input.on("close", close)
output.on("close", close)
output.on("finish", close)
function onReadyIn() {
readyIn = true
if (readyOut) writeData()
}
function onReadyOut() {
readyOut = true
if (readyIn) writeData()
}
function error(e) {
cleanup()
if (readyIn) input.destroy(e)
if (readyOut) output.destroy(e)
}
function close() {
cleanup()
if (readyIn) input.destroy()
if (readyOut) output.destroy()
}
function cleanup() {
input.removeListener("error", error)
output.removeListener("error", error)
input.removeListener("ready", onReadyIn)
output.removeListener("ready", onReadyOut)
} to this: // New with promises
let input, output
try {
input = await fsPromises.createReadStream(infile)
output = await fsPromises.createReadStream(outfile)
// write data
} finally {
if (input != null) { input.destroy(); await input.closePromise }
if (output != null) { output.destroy(); await output.closePromise }
} I don't like the name |
This is already possible given that a stream.Readable can be used as an async generator and a Promises version of fileHandle.write() exists. Closing. |
Basically, I'd like to see those two common core APIs ported to use promises for the initial
ready
/error
.This amounts to the vast majority of my routine promise event wrappers while using
fs/promises
normally.The text was updated successfully, but these errors were encountered: