@lyzkov
Most (if not all) of the snippets posted in this thread have some rough edges (though I am not criticizing any solution, since some things were just not available back then):
- Using
Foundationis not necessary (anymore). You can useTask.sleep(until:tolerance:clock:). Rshould conform toSendable. This is a requirement ofTaskGroupand it warns you if you use strict concurrency.- If you cancel the
Taskin whichwithTimeoutwas started it could swallow the cancellation behavior ofoperationand just throwCancellationErrorfrom theTask.sleepbecauseTaskGroupwill forward the first error it receives from its children. - The
try Task.checkCancellation()is awkward, as @ktoso already mentioned here: