Skip to content

Instantly share code, notes, and snippets.

@TimWhiting
Last active July 9, 2022 18:26
Show Gist options
  • Save TimWhiting/c2f510af1fdc729c4fc28f00ce253573 to your computer and use it in GitHub Desktop.
Save TimWhiting/c2f510af1fdc729c4fc28f00ce253573 to your computer and use it in GitHub Desktop.
cached_future
import 'dart:async';
void main() {
final s = Service();
Future.wait([for (final _ in [0, 1, 2, 3, 4 ]) s.setup()]);
}
class Service {
Future<void> setup() async {
if (_completer == null) {
_completer = Completer();
// Do setup
print('Start setup');
await Future.delayed(Duration(seconds: 1));
_completer!.complete(null);
print('Finished setup');
return;
}
print('returning future');
return _completer!.future;
}
Completer<void>? _completer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment