Difference between .then() and .whenCompleted() methods when working with Futures?GWT vs Dart - what are the main differences? Is Dart a potential replacement of GWT?Completer and Future in dart?Launching multiple async futures in response to eventsDart - running a project does not load the dart file and not printing in the consoleDifference between a Future returning another Future and NotUsing Dart in Chrome extension content script does not run?Future execution leads to unhandled exceptionDart AppEngine Headers already sent when using FuturesCan I call any dart function inside aqueduct Response controller?How to Determine in Dart when a method is Overridden?
Defense against attacks using dictionaries
Are illustrations in novels frowned upon?
Why don't we use Cavea-B
IndexOptimize - Configuration
Why is 日本 read as "nihon" but not "nitsuhon"?
How to draw a square on cylinder?
Sleeping solo in a double sleeping bag
Factoring the square of this polynomial?
Is there a known non-euclidean geometry where two concentric circles of different radii can intersect? (as in the novel "The Universe Between")
LeetCode: Pascal's Triangle C#
Co-author responds to email by mistake cc'ing the EiC
Why would the US President need briefings on UFOs?
Is there such a thing as too inconvenient?
System to validate run time complexity requirements
How to compare two different formulations of a problem?
Shouldn't the "credit score" prevent Americans from going deeper and deeper into personal debt?
How to persuade recruiters to send me the Job Description?
Was Switzerland really impossible to invade during WW2?
Don't understand MOSFET as amplifier
Were there 486SX revisions without an FPU on the die?
When translating the law, who ensures that the wording does not change the meaning of the law?
Do ability scores have any effect on casting Wish spell
In an emergency, how do I find and share my position?
Fancy String Replace
Difference between .then() and .whenCompleted() methods when working with Futures?
GWT vs Dart - what are the main differences? Is Dart a potential replacement of GWT?Completer and Future in dart?Launching multiple async futures in response to eventsDart - running a project does not load the dart file and not printing in the consoleDifference between a Future returning another Future and NotUsing Dart in Chrome extension content script does not run?Future execution leads to unhandled exceptionDart AppEngine Headers already sent when using FuturesCan I call any dart function inside aqueduct Response controller?How to Determine in Dart when a method is Overridden?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm exploring Futures in Dart, and I'm confused about these two methods that Future offers. Whats the main difference between them?
Lets say I want to read a .txt using .readAsString()
, I would do it like this:
void main()
File file = new File('text.txt');
Future content = file.readAsString();
content.then((data)
print(content);
);
So .then()
is like a callback that fires a function once Future is completed.
But I see there is also .whenComplete()
that can also fire an function once Future completes. Something like this :
void main()
File file = new File('text.txt');
Future content = file.readAsString();
content.whenComplete(()
print("Completed");
);
Difference I see here is that .then()
has access to data that was returned!
What is .whenCompleted()
used for? When to chose one over the other?
And also on these two links, .then() and .whenCompleted() at the end of a page there are implementations:
.then():
Future<R> then<R>(FutureOr<R> onValue(T value), Function onError);
.whenCompleted():
Future<T> whenComplete(FutureOr action());
What does Future<R>
means? Or Future<T>
? I get that Future is a type, but what are R and T?
Thanks!
dart
add a comment |
I'm exploring Futures in Dart, and I'm confused about these two methods that Future offers. Whats the main difference between them?
Lets say I want to read a .txt using .readAsString()
, I would do it like this:
void main()
File file = new File('text.txt');
Future content = file.readAsString();
content.then((data)
print(content);
);
So .then()
is like a callback that fires a function once Future is completed.
But I see there is also .whenComplete()
that can also fire an function once Future completes. Something like this :
void main()
File file = new File('text.txt');
Future content = file.readAsString();
content.whenComplete(()
print("Completed");
);
Difference I see here is that .then()
has access to data that was returned!
What is .whenCompleted()
used for? When to chose one over the other?
And also on these two links, .then() and .whenCompleted() at the end of a page there are implementations:
.then():
Future<R> then<R>(FutureOr<R> onValue(T value), Function onError);
.whenCompleted():
Future<T> whenComplete(FutureOr action());
What does Future<R>
means? Or Future<T>
? I get that Future is a type, but what are R and T?
Thanks!
dart
add a comment |
I'm exploring Futures in Dart, and I'm confused about these two methods that Future offers. Whats the main difference between them?
Lets say I want to read a .txt using .readAsString()
, I would do it like this:
void main()
File file = new File('text.txt');
Future content = file.readAsString();
content.then((data)
print(content);
);
So .then()
is like a callback that fires a function once Future is completed.
But I see there is also .whenComplete()
that can also fire an function once Future completes. Something like this :
void main()
File file = new File('text.txt');
Future content = file.readAsString();
content.whenComplete(()
print("Completed");
);
Difference I see here is that .then()
has access to data that was returned!
What is .whenCompleted()
used for? When to chose one over the other?
And also on these two links, .then() and .whenCompleted() at the end of a page there are implementations:
.then():
Future<R> then<R>(FutureOr<R> onValue(T value), Function onError);
.whenCompleted():
Future<T> whenComplete(FutureOr action());
What does Future<R>
means? Or Future<T>
? I get that Future is a type, but what are R and T?
Thanks!
dart
I'm exploring Futures in Dart, and I'm confused about these two methods that Future offers. Whats the main difference between them?
Lets say I want to read a .txt using .readAsString()
, I would do it like this:
void main()
File file = new File('text.txt');
Future content = file.readAsString();
content.then((data)
print(content);
);
So .then()
is like a callback that fires a function once Future is completed.
But I see there is also .whenComplete()
that can also fire an function once Future completes. Something like this :
void main()
File file = new File('text.txt');
Future content = file.readAsString();
content.whenComplete(()
print("Completed");
);
Difference I see here is that .then()
has access to data that was returned!
What is .whenCompleted()
used for? When to chose one over the other?
And also on these two links, .then() and .whenCompleted() at the end of a page there are implementations:
.then():
Future<R> then<R>(FutureOr<R> onValue(T value), Function onError);
.whenCompleted():
Future<T> whenComplete(FutureOr action());
What does Future<R>
means? Or Future<T>
? I get that Future is a type, but what are R and T?
Thanks!
dart
dart
asked Mar 27 at 15:43
MatijaMatija
1,0065 silver badges12 bronze badges
1,0065 silver badges12 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
.whenComplete will fire a function either when the Future completes with an error or not, instead .then will fire a function after the Future completes without an error.
Quote from the .whenComplete API DOC
This is the asynchronous equivalent of a "finally" block.
Thanks Mattia, and what type is <R> and <T>?
– Matija
Mar 27 at 17:50
1
They are not actual types, but generics.
– Mattia
Mar 27 at 18:05
Thank you, this helped me a lot!
– Matija
Mar 27 at 18:58
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55381236%2fdifference-between-then-and-whencompleted-methods-when-working-with-future%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
.whenComplete will fire a function either when the Future completes with an error or not, instead .then will fire a function after the Future completes without an error.
Quote from the .whenComplete API DOC
This is the asynchronous equivalent of a "finally" block.
Thanks Mattia, and what type is <R> and <T>?
– Matija
Mar 27 at 17:50
1
They are not actual types, but generics.
– Mattia
Mar 27 at 18:05
Thank you, this helped me a lot!
– Matija
Mar 27 at 18:58
add a comment |
.whenComplete will fire a function either when the Future completes with an error or not, instead .then will fire a function after the Future completes without an error.
Quote from the .whenComplete API DOC
This is the asynchronous equivalent of a "finally" block.
Thanks Mattia, and what type is <R> and <T>?
– Matija
Mar 27 at 17:50
1
They are not actual types, but generics.
– Mattia
Mar 27 at 18:05
Thank you, this helped me a lot!
– Matija
Mar 27 at 18:58
add a comment |
.whenComplete will fire a function either when the Future completes with an error or not, instead .then will fire a function after the Future completes without an error.
Quote from the .whenComplete API DOC
This is the asynchronous equivalent of a "finally" block.
.whenComplete will fire a function either when the Future completes with an error or not, instead .then will fire a function after the Future completes without an error.
Quote from the .whenComplete API DOC
This is the asynchronous equivalent of a "finally" block.
answered Mar 27 at 16:40
MattiaMattia
9641 gold badge6 silver badges17 bronze badges
9641 gold badge6 silver badges17 bronze badges
Thanks Mattia, and what type is <R> and <T>?
– Matija
Mar 27 at 17:50
1
They are not actual types, but generics.
– Mattia
Mar 27 at 18:05
Thank you, this helped me a lot!
– Matija
Mar 27 at 18:58
add a comment |
Thanks Mattia, and what type is <R> and <T>?
– Matija
Mar 27 at 17:50
1
They are not actual types, but generics.
– Mattia
Mar 27 at 18:05
Thank you, this helped me a lot!
– Matija
Mar 27 at 18:58
Thanks Mattia, and what type is <R> and <T>?
– Matija
Mar 27 at 17:50
Thanks Mattia, and what type is <R> and <T>?
– Matija
Mar 27 at 17:50
1
1
They are not actual types, but generics.
– Mattia
Mar 27 at 18:05
They are not actual types, but generics.
– Mattia
Mar 27 at 18:05
Thank you, this helped me a lot!
– Matija
Mar 27 at 18:58
Thank you, this helped me a lot!
– Matija
Mar 27 at 18:58
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55381236%2fdifference-between-then-and-whencompleted-methods-when-working-with-future%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown