Cancelling async iterators
See original GitHub issueSince redux-saga doesn’t directly support async iterators, I was wondering how we can deal with third-party dependencies that provide an async iterator as their interface, and specifically how we may cancel iteration before the iterator is exhausted.
The standard way to do stop an asyncIterator is to simply break
out of the for await (... of ...)
loop that’s consuming it. However the standard way to cancel tasks in redux-saga is to give it a cancel(task)
effect, which will run the return
method on the saga generator. This would mean that in each iteration of the for await
loop, we’d need to have a yield
that allows redux-saga to intercede with the cancellation. However, as previously noted, redux-saga doesn’t support async function*
, so there’s no obvious way to feed that cancellation into the loop.
I was wondering if there was any recommended best practice for dealing with this cancellation problem - preferably one that doesn’t involve a convoluted dance with a global state-management system. Please note that in my use case, it is imperative that the iterator stop execution; it’s not sufficient to leave the loop detached and continue to iterate while redux-saga ignores it.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:5 (3 by maintainers)
As far as I know, there is no way to impact the async iterator while it is “in flight”. The item has to resolve first before you can do anything about it. For that reason, I wouldn’t recommend iterating through it using
for-await-of
loop but rather doing something like that:Note that I don’t have any practical experience with async iterators and I could be wrong about this.
Thanks for posting this issue! I’d love to read thoughts from others on this issue. I have not specifically had this issue but if I’m reading this correctly, I think it is something redux-saga should address.