0

I have a Dart function that looks like:

Future beAwesome() {
  if (notActuallySupported) {
    return new Future.error(new UnsupportedError('uh oh'));
  }

  return new Future.value(42);
}

// ...

beAwesome().then((answer) => print(answer));

I want to use the new async/await functionality. How do I change my function?

1 Answer 1

3

In general, add the word async after your function's signature and before the {. Also, return raw values instead of wrapping those values in futures. Also, throw actual exceptions instead of wrapping the errors with a future.

Here's the new version:

Future beAwesome() async {
  if (notActuallySupported) {
    throw new UnsupportedError('uh oh');
  }

  return 42;
}

// ...

var answer = await beAwesome();
print(answer);

Note that you should still use Future as the return-type annotation.

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.