Skip to content

Instantly share code, notes, and snippets.

@victorers1
Last active May 28, 2022 20:06
Show Gist options
  • Save victorers1/3a822687b5e111457b90a79b2e7e9f9d to your computer and use it in GitHub Desktop.
Save victorers1/3a822687b5e111457b90a79b2e7e9f9d to your computer and use it in GitHub Desktop.
Studying Future in Dart language
void main() async {
/// Both functions init at the same time, Future.any() will return the first one available
final dynamic firstResult = await Future.any([asyncF(), asyncG()]);
/// firstResult can be an integer or a string, thus the 'dynamic' type.
print('$firstResult. Returned value is a ${firstResult.runtimeType}');
}
Future<int> asyncF() async {
print('asyncF init');
await Future.delayed(Duration(seconds: 2));
return 2;
}
Future<String> asyncG() async {
print('asyncG init');
await Future.delayed(Duration(seconds: 1));
return 'asyncG is faster';
}
List<int> inteiros = [];
void main() async {
print('inteiros iniciais = $inteiros');
/// Future.doWhile returns null when completed
/// When the action returns true, it's re-called
final dynamic result = await Future.doWhile(asyncF);
print('1º doWhile ended with result = $result');
print('inteiros = $inteiros');
await Future.doWhile(syncF).whenComplete(
() {
print('2º doWhile ended');
},
);
print('inteiros = $inteiros');
}
Future<bool> asyncF() async {
inteiros.add(inteiros.length);
await Future.delayed(Duration(seconds: inteiros.length));
return inteiros.length <= 2;
}
bool syncF() {
inteiros.add(inteiros.length);
return inteiros.length <= 10;
}
void main() async {
/// Both functions init at same time
/// When every async function has completed, the list is generated with returned values in correct order
final List f = await Future.wait([asyncF(), asyncG()]);
//Even if the async functions doesn't end the same time, `Future,wait` returns all values together
print(f);
}
Future<int> asyncF() async {
print('asyncF init');
await Future.delayed(Duration(seconds: 2));
print('asyncF end');
return 1;
}
Future<int> asyncG() async {
print('asyncG init');
await Future.delayed(Duration(seconds: 1));
print('asyncG end');
return 2;
}
/**
* Output is:
* asyncF init
* asyncG init
* asyncG end
* asyncF end
* [1, 2]
*/
void main() {
/// The forEach's second argument is a async function that will execute receiving each element in the iterator
Future.forEach(
['1', 2],
(dynamic value) async {
final int durationInSeconds = int.parse(value.toString());
await Future.delayed(Duration(seconds: durationInSeconds));
print('$value of type ${value.runtimeType}');
},
).whenComplete(
() => print('End of async forEach'),
);
Future.forEach(
['1', 2],
syncF,
).whenComplete(
() => print('End of sync forEach'),
);
}
/// Return values are discarted by Future.forEach()
dynamic syncF(dynamic value) {
print('$value of type ${value.runtimeType}');
return value;
}
/**
* Output is:
* 1 of type String
* 2 of type int
* End of sync forEach
* 1 of type String
* 2 of type int
* End of async forEach
*/
void main() async {
/// whenComplete doesn't dicarts Future's value
final value = await Future.value(1).whenComplete(
() {
print('1º Future ended');
},
);
print('value is $value');
/// then() can override Future's value
final String newValue = await Future.value(2).then(
(newValue) {
print('newValue inside then is $newValue, type: ${newValue.runtimeType}');
return '3';
},
).whenComplete(
() {
print('2º Future ended');
},
);
print('newValue outside then is $newValue, of type ${newValue.runtimeType}');
/// If whenComplete's actions is async, the Future.value completion is delayed until it the action is done
final bool delayedValue = await Future.value(true).whenComplete(
() async {
print('3º Future ended but I\'m delaying it');
await Future.delayed(Duration(seconds: 4));
},
);
print('delayedValue is $delayedValue');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment