I don't think we are talking about the same thing. I think it is totally fine to implement these interfaces yourself: usually when you implement a producer in a "low level" way you know what you are doing (cf Nodejs core maintainers).
I am talking about the code which consumes it, depending on the way you consume it, you might create leaks.
Consider the following producer (from my article I linked above):
constwait=delay=>newPromise(resolve=>{setTimeout(()=>resolve(),delay);});constcounterGen=asyncfunction*(limit=10,delay=100){letiter=1;try{while(true){if(iter>limit){break;}awaitwait(delay);yielditer;iter++;}}catch(e){console.log('oops something is wrong');throwe;}finally{console.log('I have been released !!!');}};
You could have written it by implementing the interfaces, it does not matter.
Just note it is doing some cleaning ("I have been released !!!"), it could be release file handle or whatever.
Now let's say you want to consume it and sum the 3 first values.
You can do it in a naive way
constsum=asynciterator=>{leti=0;letsum=0;while(i<3){constnext=awaititerator.next();if(next.done){break;}sum+=next.value;i++;}// VERY IMPORTANT IF YOU DO NOT WANT TO CREATE A LEAK// iterator.return();returnsum;};sum(counterGen()).then(console.log);
And this code creates a leak if you don't pay attention and do not explicitly call return (you will not see the release message)
Whereas if you decide to go for "native construct" like for await statement you are safe
I don't think we are talking about the same thing. I think it is totally fine to implement these interfaces yourself: usually when you implement a producer in a "low level" way you know what you are doing (cf Nodejs core maintainers).
I am talking about the code which consumes it, depending on the way you consume it, you might create leaks.
Consider the following producer (from my article I linked above):
You could have written it by implementing the interfaces, it does not matter.
Just note it is doing some cleaning ("I have been released !!!"), it could be release file handle or whatever.
Now let's say you want to consume it and sum the 3 first values.
You can do it in a naive way
And this code creates a leak if you don't pay attention and do not explicitly call
return
(you will not see the release message)Whereas if you decide to go for "native construct" like
for await
statement you are safeI think tutorials and articles on iterators (and async iterators) do not stress enough that eventual issue.
This was my point :)
Uh I've totally misunderstood your previous message!
Yes you are right, the
for-of
and thefor-await-of
should be always preferred 🙂