I have a 'store' class which dispatches actions. The dispatch is async because in some cases it’s very useful to wait until all the side effects of dispatching an action are complete (eg. for testing). But in normal use, the dispatch method is fire-and-forget.
To avoid the extra call site Task {}
boilerplate I found I could add a second non-async method, which I thought would give callers the option of whether to await
the dispatch or not, depending on their needs.
@MainActor public final class Store {
public func dispatch(_ action: Action) {
Task { try await _dispatch(action) }
}
public func dispatch(_ action: Action) async throws {
try await _dispatch(action)
}
// ...
}
But it seems like I’m misunderstanding how the sync/async method resolution is designed. It seems that from an async context I’m forced to use the async dispatch method? For example, the following fails to compile:
@MainActor func testSyncDispatch() async throws {
Store().dispatch(SomeAction()) // Expression is 'async' but is not marked with 'await'
}
I’m guessing I just have the wrong mental model with this. Am I missing something obvious? Is there a better way to structure this API?