Closed philipnilsson closed 1 month ago
Async iterators already have a cleanup method in the form of .return
, which is called whenever a for-of early exits. So if readLines
has an appropriate implementation (i.e., it returns an iterable iterator which cleans itself up on completion and when its return
method is called), then
for await (const line of readlines('myFile.txt')) {
//
}
should do the right thing already, even without this proposal.
Correct. In addition, per the spec the Symbol.dispose
/Symbol.asyncDispose
methods of built-in iterators/generators invoke return
, so the using
is redundant in this case. The main reason to use using
with iterators is to help with manual iteration, i.e.:
using iter = readlines('myfile.txt');
for (let res = iter.next(); !res.done; res = iter.next()) {
...
}
I'm using this proposal via the implementation currently available in TypeScript, and a pattern I've found myself often wanting to use is to have
AsyncIterables
that are alsoDisposable
/AsyncDisposable
. An example could be a function that asynchronously yields the contents of a file, line by line. This would of course also need to dispose of the open file at completion. I'm currently writing this aswhere
lineReader
has implementations of both a[Symbol.asyncIterator]
and a[Symbol.disposable]
(@@asyncIterator
/@@dispose
in your terminology?)I'm wondering if there's a better way of doing this. My concern is that the first line is a bit redundant, and easy to forget. Ideally I'd love to just write something along the lines of
I see there is syntax in this proposal allowing the
using
statement inside the loop expression, which is fine but seems very rare in comparison to wanting the resource management to be tied to the expression producing the async-iterable. Am I missing something here, or would this be recommended usage?There also seems to be an opportunity here in allowing the cleanup of the resource immediately after the for-loops completion, which isn't the case in my first example. Of course you could wrap that in another block, but that further reduces the ergonomics.