async icon indicating copy to clipboard operation
async copied to clipboard

Async Cache is caching exceptions

Open akmalviya03 opened this issue 2 years ago • 5 comments

Steps to reproduce

To reproduce the same issue you can refer the code sample given below.

Expected results

It should only cache successful result.

Actual results

It should not cache exception.

This callback is running
We have successfully cancelled future
Error First Exception: Future Cancelled
Error Second Exception: Future Cancelled

Code sample

Code sample
import 'package:async/async.dart';

late AsyncCache<String> temporaryCache;
late CancelableOperation<String> cancelableOperation;

Future<void> main(List<String> arguments) async {
  temporaryCache = AsyncCache(Duration(seconds: 15));
  cancelableOperation =
      CancelableOperation.fromFuture(_futureToRun(), onCancel: () {
    print('We have successfully cancelled future');
  });
   getData().then((value) => print('First')).onError((error, stackTrace) => print('Error First $error'));
  cancelableOperation.cancel();
  getData().then((value) => print('Second')).onError((error, stackTrace) => print('Error Second $error'));
}

Future<String> _futureToRun() async {
  await Future.delayed(const Duration(seconds: 5));
  return Future.value("Temporary Cache");
}

Future<String> getData() async {
  return temporaryCache.fetch(() async {
    print('This callback is running');
    String? value = await cancelableOperation.valueOrCancellation();
    if (value != null) {
      return value;
    }
    throw Exception('Future Cancelled');
  });
}

akmalviya03 avatar Dec 05 '23 09:12 akmalviya03