question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cancelling async iterators

See original GitHub issue

Since 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:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Andaristcommented, Oct 14, 2022

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:

function* getAllPages(octokit, request) {
  let results = [];

  for (const responseP of octokit.paginate.iterator(request)) {
    const { value, done } = yield responseP
    if (done) {
      break;
    }
    results = results.concat(value.body);
  }

  return results;
};

Note that I don’t have any practical experience with async iterators and I could be wrong about this.

1reaction
neurosnapcommented, Feb 27, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using cancellation with async iterators in JavaScript
This blog post demonstrates a particular use case and delves into some details about how promise cancellation works with async iterators.
Read more >
Is this Promise cancellation implementation for reducing an ...
I found the way, Promise.race is like a secret weapon or something if (isAsyncIterable(x)) { const state = { cancel: () => {}...
Read more >
AsyncSequence, break, and cancellation - Using Swift
AsyncIterator has no cancel method, so iter.cancel() is not possible. The Swift async runtime can communicate with the iterator in two ways only ......
Read more >
Async Enumerables with Cancellation - Julien Couvreur
If the async enumerable instance is given to you, or is constructed in a way to doesn't capture the desired cancellation token, then...
Read more >
Cancellable async functions in JavaScript - DEV Community ‍ ‍
Hooray! Now, you can call fetchAndFlash() from wherever you like, and know that any previous calls will cancel as soon as possible. Aside ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found