Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between returning void vs returning Future<void>?

Is there a difference between an async method that returns void, and one that returns Future<void>? It seems that both are valid in Dart:

void main() async {
    await myVoid();
    await myFutureVoid();
}


void myVoid() async {
    // Do something
}


Future<void> myFutureVoid() async {
    // Do something
}

Are they identical?

If so, why is void allowed when for example int is not? The compiler says "Functions marked 'async' must have a return type assignable to 'Future'".

like image 603
Magnus Avatar asked May 21 '19 18:05

Magnus


People also ask

What is the difference between a void and a void return?

Void means nothing is returned, and so trying to return something from a void function is illegal. On the other hand, not returning something using the return statement is illegal if the function is not void. I'm going to assume you meant “what is the difference between void and other return types in C++”.

Why do void functions return none in Python?

Moreover, it is generally assigned to a variable for denoting that the variable does not point to any object. Through the program given below, you can see how it shows that void functions return None: Moreover, add () function certainly returns None.

What is a void function in C++?

Void functions, also called nonvalue-returning functions, are used just like value-returning functions except void return types do not return a value when the function is executed.

What is the return type of a function that never returns?

Because the function never returns, the return type doesn’t really matter. You can use any type — even the absurd bottom type, which can be used in various ways in various languages. C++ has this as noreturn. Rust has !, and Scala has Nothing.


1 Answers

void f() and Future<void> f() are not identical. (The presence of the async keyword doesn't actually matter. The async keyword primarily enables the use of the await keyword in the function body.)

void f() declares a function that returns nothing. If it does asynchronous work, then that work will be "fire-and-forget": there is no opportunity for the caller of f to wait for it to finish.

In contrast, Future<void> f() declares a function that returns a Future that the caller can wait for (either by using await or by registering a Future.then() callback). There's no value returned by the asynchronous work, but callers can determine when it is finished.

Functions marked with async usually should return a Future. If you have a function that does asynchronous work that produces an actual value (such as an int), then the caller must wait for that value to be computed before it can be used. That function therefore must return a Future.

As a special case, an async function can return void instead of Future<void> to indicate that it is fire-and-forget.

like image 106
jamesdlin Avatar answered Sep 29 '22 06:09

jamesdlin