- Ensures serial execution of asynchronous tasks.
- Maintains task order, executing tasks in the sequence they were added.
- Tasks exceptions don't blocking the entire execution queue.
- Supports returning results from tasks.
To use SerialTaskExecutor
in your Dart project, add it as a dependency in your pubspec.yaml
file:
dependencies:
serial_task_executor: ^0.0.1
Then import the package in your Dart code:
import 'package:serial_task_executor/serial_task_executor.dart';
Create an instance of SerialTaskExecutor:
final executor = SerialTaskExecutor();
Future<SomeOperationResult> runSyncOperationSequence() {
return executor.executeTask(_runSyncOperationSequence);
}
Future<SomeOperationResult> _runSyncOperationSequence() async {
await _upSyncOperation();
await _downSyncOperation();
return _getSyncResult();
}
Each task will be executed in the order they were added, and the next task will not start until the current one is completed.
Errors in tasks can be handled using standard try-catch blocks or Future error handling mechanisms:
executor.executeTask(() async {
try {
// Task code that might throw
} catch (e) {
// Error handling
}
});
Notably, an unhandled error will not affect the sequential execution of other tasks in the queue. All tasks in the queue will be executed in the order they were added, even if one or more tasks throw an unhandled exception.