pointfreeco / swift-concurrency-extras

Useful, testable Swift concurrency.
MIT License
343 stars 23 forks source link

Propose Task.delay for delayed execution #7

Closed Ryu0118 closed 1 year ago

Ryu0118 commented 1 year ago

I propose extensions to Task type that delay the execution of async operation.

mbrandonw commented 1 year ago

Hi @Ryu0118, can you provide some examples of how you would use this tool? It doesn't seem too different from just doing this inline:

try await Task.sleep(for: .seconds(1))
await …

Further, using Task.delay leads you to code that is difficult to test since a clock is not involved. It is better to do:

@Dependency(\.continuousClock) var clock
try await clock.sleep(for: .seconds(1))
await …

Also, spinning up unstructured tasks will mean losing out on cooperative cancellation, so you would want to bridge that too.

stephencelis commented 1 year ago

@Ryu0118 We're going to close this out for now, but if you follow up with some more info and motivation we can reconsider. Thanks for taking the time to PR!