Skip to content

Add "asyncOr", similar to "async", for FutureOr #2033

Open
@rrousselGit

Description

@rrousselGit

A common UI problem is to want to make potentially asynchronous code synchronous if possible, to avoid flickers where progress indicators show only for a frame or two.

While there are solutions to this problem, the issue is, those solutions are strictly incompatible with async/await – decreasing the code readability quite a bit.

Proposal

To solve the problem of async/await being incompatible with code-paths that are potentially synchronous, we could introduce an async variant that returns a FutureOr

Meaning we could write:

FutureOr<int> multiplyByTwo() keyword {
  FutureOr<int> futureOr;
  T value = await futureOr;
  return value * 2;
}

With this variant, await may execute synchronously (when it currently would always be async). Meaning that the previous snippet would be equivalent to doing:

FutureOr<int> multiplyByTwo() {
  FutureOr<int> futureOr;
  if (futureOr is Futureint>)
    return futureOr.then((value) => value * 2);
  return value * 2;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureProposed language feature that solves one or more problems

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions