You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add variant of fs.create{Read,Write}Stream to fs/promises #21342
Basically, I'd like to see those two common core APIs ported to use promises for the initial ready/error.
// Naïve implementation, but could be very dramatically improved.// `createWriteStream` is similar.functioncreateReadStream(...args){returnnewPromise((resolve,reject)=>{conststream=fs.createReadStream(...args)functionready(){stream.removeListener("ready",ready)stream.removeListener("error",fail)resolve(stream)}functionfail(e){stream.removeListener("ready",ready)stream.removeListener("error",fail)reject(e)}stream.on("ready",ready)stream.on("error",fail)})}
This amounts to the vast majority of my routine promise event wrappers while using fs/promises normally.
I would prefer this to be a chance to change the readable/writeable streams returned by fs module.
E.g. something like a .promise property on readable and writable streams that resolves on close for writeable and end for readable and rejects on error. #20909 is related.
@ChALkeR I would be mildly okay with that, too, but I'd still like the ready/error issue also resolved. And yes, #20909 is one of the driving reasons for me filing this bug. (It's the most common case where I need that feature, and I've written a dedicated helper for this probably a few dozen times already.)
Using this + your addition would convert this:
// Old with callbacksconstinput=fs.createWriteStream(infile)constoutput=fs.createWriteStream(outfile)letreadyIn=falseletreadyOut=falseinput.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)functiononReadyIn(){readyIn=trueif(readyOut)writeData()}functiononReadyOut(){readyOut=trueif(readyIn)writeData()}functionerror(e){cleanup()if(readyIn)input.destroy(e)if(readyOut)output.destroy(e)}functionclose(){cleanup()if(readyIn)input.destroy()if(readyOut)output.destroy()}functioncleanup(){input.removeListener("error",error)output.removeListener("error",error)input.removeListener("ready",onReadyIn)output.removeListener("ready",onReadyOut)}
to this:
// New with promisesletinput,outputtry{input=awaitfsPromises.createReadStream(infile)output=awaitfsPromises.createReadStream(outfile)// write data}finally{if(input!=null){input.destroy();awaitinput.closePromise}if(output!=null){output.destroy();awaitoutput.closePromise}}
I don't like the name .promise, though - it's too generic for that specific of a thing. It's also not FS-specific. Also, if you're going to do that, you may want to include close for readable, too, since that's what fires when you .destroy a readable stream.
Activity
simonkcleung commentedon Jun 16, 2018
To clean up the resource, you should use finally, which can also address the problem when you throw an error.
dead-claudia commentedon Jun 16, 2018
@simonkcleung This isn't about cleanup. It's about errors during initialization.
ChALkeR commentedon Jun 16, 2018
I would prefer this to be a chance to change the readable/writeable streams returned by fs module.
E.g. something like a
.promiseproperty on readable and writable streams that resolves onclosefor writeable andendfor readable and rejects onerror. #20909 is related.dead-claudia commentedon Jun 17, 2018
@ChALkeR I would be mildly okay with that, too, but I'd still like the
ready/errorissue also resolved. And yes, #20909 is one of the driving reasons for me filing this bug. (It's the most common case where I need that feature, and I've written a dedicated helper for this probably a few dozen times already.)Using this + your addition would convert this:
to this:
I don't like the name
.promise, though - it's too generic for that specific of a thing. It's also not FS-specific. Also, if you're going to do that, you may want to includeclosefor readable, too, since that's what fires when you.destroya readable stream.jasnell commentedon Jun 19, 2020
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.