Skip to content

Commit 508b9bc

Browse files
committed
feat: support creating iterator async
Allows early errors before the stream is created: ```js async function * read (fd) { // read file here } const createReadStream = asyncIteratorToStream(async function (file) { const fd = await fs.open(file, 'r') return read(fd) }) try { const stream = await createReadStream('foo.txt') stream.pipe(process.stdout) } catch (error) { console.error('failed to open the file', error) } ```
1 parent ff9a640 commit 508b9bc

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ function asyncIteratorToStream (iterable, options) {
6868
}
6969
}
7070

71+
const { then } = iterable
72+
if (typeof then === 'function') {
73+
return then.call(iterable, iterable =>
74+
asyncIteratorToStream(iterable, options)
75+
)
76+
}
77+
7178
const iterator = resolveToIterator(iterable)
7279
const readable = options instanceof Readable ? options : new Readable(options)
7380
readable._destroy = async (error, cb) => {

0 commit comments

Comments
 (0)