Skip to content

Instantly share code, notes, and snippets.

@SergeShkurko
Created October 10, 2025 14:21
Show Gist options
  • Select an option

  • Save SergeShkurko/5800102ab038d9fc02213ebbb1439ac7 to your computer and use it in GitHub Desktop.

Select an option

Save SergeShkurko/5800102ab038d9fc02213ebbb1439ac7 to your computer and use it in GitHub Desktop.
Test exercise for Flutter (Race condition)
class Result {
final dynamic value;
final bool isSuccess;
Result.success(this.value) : isSuccess = true;
Result.error() : value = null, isSuccess = false;
}
Future<List<Result>> allSettled(List<Future> futures) async {
return [];
}
void main() async {
final futures = [
Future.delayed(Duration(seconds: 3), () => 100),
Future.delayed(Duration(seconds: 1), () => 200),
Future.delayed(Duration(seconds: 2), () => throw 'Error'),
Future.delayed(Duration(milliseconds: 500), () => 300),
];
final results = await allSettled(futures);
// Should return results in order: index 0, 1, 2, 3
for (var i = 0; i < results.length; i++) {
print('Index $i: ${results[i].isSuccess ? results[i].value : 'ERROR'}');
}
// Expectet output:
// Index 0: 100
// Index 1: 200
// Index 2: ERROR
// Index 3: 300
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment