-
Notifications
You must be signed in to change notification settings - Fork 29
Allow auto-cancelation inside an async function at every await point #23
Comments
Two questions:
|
Promise.race seems irrelevant. You can only cancel after the read or delay is settled anyway. Before that there's no way for code outside the function run since the async function itself is running.
Please read the spec... more like the latter. |
I'm not sure what you mean. Let me explain with an example:
Will "done" be logged after one or two seconds? Clearly there is code running concurrently to the async function and the two-second delay. |
Two seconds. Because here is what the VM does:
NOW execution of the function is suspended for 2 seconds, and we can execute other code (such as And yes, (This is why it is so hard to find a good use case for |
Sure there is - that's what we registered the
which would cause "done" to be logged after only 1s in the above example. Isn't that what we want, to break and run our |
That's an interesting proposal as well, thank you. Although Promise.race only takes one argument, and ignores canceled promises anyway, I think I understand what you're getting at. |
Ah, really should've studied https://domenic.github.io/cancelable-promise/#sec-promise.race better, I was under the mistaken assumption that cancellation worked like rejection there. |
Just a quick update, I've changed The best we could get from a clarity perspective might be a scope-like behaviour: await.cancellation from cancelToken in {
await …;
} (but we would need to consider what to do with nesting, maybe even allow token composition through that) |
The reason it's going to be specified as await.cancelToken = is because it fits with the existing meta-property concept used elsewhere. There is no dotted unary operator concept so we won't be introducing one. |
You sound like it's decided already? |
Yeah, it was decided at the last TC39 meeting. |
Ah, I had interpreted the meeting notes as only pursuing this path further, not to settle on the exact syntax. Anyway, I should be glad my idea was adopted :-) |
Wow, thanks! I guess I should open seperate issues for any problems I see with that draft or whishes I have to extend it? |
Yeah, that would be best. |
I am extracting this from #19, which in general is about an entirely different proposal unrelated to this repo (and thus closed), but inspired an idea which could be useful for this repo.
The idea is to have syntax sugar for automatically inserting
cancelToken.cancelIfRequested()
after everyawait
point inside an async function. Doing this implicitly has gotten pushback in the past, but doing it explicitly seems to make sense to me. The strawman is then that we would allowas sugar for
(using a slightly-tweaked version of the motivating example for cancelIfRequested, see #16).
This may be similar to what @inikulin was asking for in #18.
/cc @wycats since I believe this might address some of his concerns.
The text was updated successfully, but these errors were encountered: