Closed getify closed 5 years ago
More thoughts:
If an async function*
does a yield pr
instead of await pr
, then CAF can cancel immediately and just discard/ignore that yield
ed promise.
Of course, await pr
in your async function*
is kinda the whole point of them... if you didn't want to use await pr
, then you could/should just use normal function*
. So suggesting this as a "work-around" would kinda just be suggesting an anti-pattern.
You don't need to use async function*
for emulating cancelable async
... that's what function *
does. There's no benefit to choosing to use async function*
for this synchronous-async pattern. So maybe CAF just doesn't need to support them because it's sorta pointless/moot to support them.
I have decided finally... no we won't support async generators. The tipping point is, yield x
in an async generator implicitly awaits
the value if it's a promise, so... IOW there's no way for CAF to truly stop/finalize an async generator instance with a Promise.race(..)
over the yielded value, the way we can do with normal generators.
For the new
async function*
async generator functions that just landed in ES2018... should CAF support wrapping them (for cancelation) the same way we wrap a regularfunction *
generators?The implementation would be fairly straightforward... when you call
it.next(..)
in_runner(..)
, you just have to test if that result is itself a promise, and if so, wait for the actual iterator result from that promise before handing off toprocessResult(..)
for processing.The problem is... just like with regular
async function
s, if anasync function*
is currentlyawait
ing a promise, doingit.return(..)
doesn't immediately abort. It schedules an abort of the function, but only takes effect after the currentawait
on a promise finishes.In other words, using CAF with an
async function*
will give the appearance of the ability to do cancelations, but it will perhaps be a surprising detail that they can't necessarily be immediately canceled the wayfunction *
generators can, depending on what theasync function*
is currently doing.That kind of surprising inconsistency might be more harmful to CAF supporting these, and maybe that means CAF shouldn't handle them. OTOH, handling them for some notion of cancelation might be better than nothing.
Anyone have any thoughts?
Illustration code: