Created with <3 with dartpad.dev.
Last active
July 9, 2022 18:26
-
-
Save TimWhiting/c2f510af1fdc729c4fc28f00ce253573 to your computer and use it in GitHub Desktop.
cached_future
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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