Support a promise paradigm
See original GitHub issueBefore
yauzl.open('file.zip', (err, zipfile) => {
if (err) throw err
zipfile.on('entry', (entry) => {
zipfile.openReadStream(entry, (err, readStream) => {
if (err) throw err
readStream.pipe(somewhere)
})
})
})
After
const zipfile = await yauzl.open('file.zip')
zipfile.on('entry', async (entry) => {
const readStream = zipfile.openReadStream(entry)
readStream.pipe(somewhere)
})
})
If yauzl.open
and zipfile.openReadStream
return Promises if no callbacks are provided, it would simplify code for people who choose to use async/await. We could remove all if (err) throw err
because unhandled errors would be thrown automatically. And we don’t have as deep nesting of the code, which makes things hard to read unless functions are extracted which may otherwise not have needed to be extracted.
Of course, if callbacks are provided then no Promise is returned and code behaves as it currently does, making this change backwards-compatible.
Node v7, which will be released soon, will make native async/await available behind flags. And Node v8 will make them available without any flags. This feature would give the option to write simpler code for anyone using those versions of Node, or any version of Node with Babel (as I’m currently doing it).
Issue Analytics
- State:
- Created 7 years ago
- Reactions:5
- Comments:13 (5 by maintainers)
Top GitHub Comments
I have published an npm module yauzl-promise which promisifies the API.
Hope it’s useful to someone.
Yes, I think wanting a Promise support just to get the Promises itself is completely valid.