Git Product home page Git Product logo

Comments (3)

brainkim avatar brainkim commented on May 28, 2024

Hmmm. So first things first: your use-case is probably best served by an async generator and not a repeater:

async function *gen() {
  await checkSomethingAsynchronously();
  yield *someOtherRepeater;
} 

Think of it like the Promise constructor: if you have a promise already then using the Promise constructor is probably a mistake. Same goes for repeaters: if you have an async iterator, you should probably be using an async generator and not a repeater. Choosing between the two can be tricky and I’ve thought about making a flow-chart for the different use-cases. It really depends on the sources that you’re pulling from. If they’re callback-based, use repeaters obviously. If they’re promises or async iterators, then it depends on how complicated the pulling algorithm is. If you’re simply pulling in series, then async generators and yield/yield* work. If you’re pulling from them concurrently, then repeaters can help.

didn't we come to the conclusion awhile back that using for..await like this is dangerous

The problem was with using async generators and for...await. Calling return on an async generator object paused at the top of a for...await loop wouldn’t work until the loop resumed, potentially causing deadlocks. The same would be true with a Repeater, but you could mitigate this issue by racing iteration with the stop promise.

from repeater.

jedwards1211 avatar jedwards1211 commented on May 28, 2024

Oh yeah, I didn't think of just using an async generator.

Calling return on an async generator object paused at the top of a for...await loop wouldn’t work until the loop resumed, potentially causing deadlocks.

I wonder if this is the case for yield * also though?

from repeater.

brainkim avatar brainkim commented on May 28, 2024

I think I checked and found that yield * propagates return into the inner iterator immediately.

from repeater.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.