Skip to content

Instantly share code, notes, and snippets.

@guiathayde
Last active May 28, 2026 14:42
Show Gist options
  • Select an option

  • Save guiathayde/8d1b446b3365562952550891730a7808 to your computer and use it in GitHub Desktop.

Select an option

Save guiathayde/8d1b446b3365562952550891730a7808 to your computer and use it in GitHub Desktop.
import 'dart:math';
int randomSeconds() {
var random = Random();
return random.nextInt(10) + 1;
}
Future<String> callWebApi01() async {
print('Start call to Web API 01...');
final delay = randomSeconds();
await Future.delayed(Duration(seconds: delay));
return 'Data 1 returned in $delay seconds';
}
Future<String> callWebApi02() async {
print('Start call to Web API 02...');
final delay = randomSeconds();
await Future.delayed(Duration(seconds: delay));
return 'Data 2 returned in $delay seconds';
}
Stream<String> delayedTextStream() async* {
for (int i = 1; i <= 5; i++) {
await Future.delayed(Duration(seconds: randomSeconds()));
yield 'Text $i';
}
}
Stream<int> allEvenNumbers() async* {
var num = 0;
while (true) {
num += 2;
yield num;
}
}
void main() async {
// var data1 = await callWebApi01();
// var data2 = await callWebApi02();
//
// var results = [data1, data2];
// print(results);
// var future1 = callWebApi01();
// var future2 = callWebApi02();
/*
final results = await Future.wait([
callWebApi01(),
callWebApi02()
]);
print(results);
*/
/*
final result = await Future.any([
callWebApi01(),
callWebApi02()
]);
print(result);
*/
/*
Stream<String> st = delayedTextStream();
st.listen((value) {
print(value);
});
*/
await for (var data in delayedTextStream()) {
print('Received $data');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment