Schedule tasks which are futures to completion#112269
Schedule tasks which are futures to completion#112269auto-submit[bot] merged 4 commits intoflutter:masterfrom
Conversation
0ef029b to
9129f94
Compare
|
Friendly ping - @chunhtai can you please take a look 🙂 |
chunhtai
left a comment
There was a problem hiding this comment.
LGTM, left a documentation nit
| /// The type argument `T` is the task's return value. Consider `void` if the | ||
| /// task does not return a value. | ||
| typedef TaskCallback<T> = T Function(); | ||
| typedef TaskCallback<T> = FutureOr<T> Function(); |
There was a problem hiding this comment.
We should add some documentation in scheduleTask method to mention the behavior.
There was a problem hiding this comment.
Thanks! Looking at scheduleTask, the doc comment mentions:
Schedules the given task with the given priority and returns a Future that completes to the task's eventual return value.
This seems to already imply that the task will complete (irregardless of whether the task is async or not). In that case do you think we should still need to update it?
There was a problem hiding this comment.
eventual return value part is a bit vague. maybe add phrase to describe the behavior more clearly.
There was a problem hiding this comment.
Done, would you like to take a look again?
Today,
scheduleTaskto does not execute the scheduled task to completion, when the task is a future. If we are providing aFuture<int> Function()toscheduleTask, the current implementation ofscheduleTaskreturns a future when this task starts, but not when it completes. To actually run the task to completion, the callsite needs to look something like the following, which is kind of awkward:I stumbled across this when using the API above but with a single
await, and I was quite confused why theunawaited_futureslint was triggering which led me to looking deeper at the issue.It would be desirable for task to be scheduled to completion, even when
taskis a function that returns a future for a simpler API. We can already opt-into this behavior "for free" when the future is handed to completer. The signature ofCompleter.completeis:From the docs (emphasis mine):
Today, we're hitting the first case, but with the correct type annotations in this PR, we could get the second.
I also tested this change in an internal presubmit (tap/476354531), and it passes tests so it doesn't look like a breaking change.
Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-new channel on Discord.