Created
June 17, 2022 09:45
-
-
Save MariaMelnik/1bee0d73920548cf9cfc143c39ec49d2 to your computer and use it in GitHub Desktop.
dart: stop future with CancelableOperation
This file contains hidden or 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 'package:async/async.dart'; | |
void main() async { | |
final cancellableOperation = CancelableOperation.fromFuture(operation()); | |
cancellableOperation.then((p0) => print('cancelable operation finished normally'), | |
onCancel: () => print('cancelable operation was canceled')); | |
await Future.delayed(Duration(seconds: 1)).then((_) { | |
cancellableOperation.cancel(); | |
print('operation cancelled'); | |
}); | |
} | |
Future<void> operation() async { | |
await Future.delayed( Duration(seconds: 1)); | |
print('1'); | |
await Future.delayed( Duration(seconds: 1)); | |
print('2'); | |
await Future.delayed( Duration(seconds: 1)); | |
print('3'); | |
await Future.delayed( Duration(seconds: 1)); | |
print('4'); | |
await Future.delayed( Duration(seconds: 1)); | |
print('5'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you can't stop function execution with CancelableOperation