Naios / continuable

C++14 asynchronous allocation aware futures (supporting then, exception handling, coroutines and connections)
https://naios.github.io/continuable/
MIT License
815 stars 44 forks source link

Continue-on-fail support - allowing chains to continue with a value even after a fail? #39

Closed noisypants closed 3 years ago

noisypants commented 3 years ago

@Naios Hello. Nice work with this library. it's been fun integrating it into our traditional callback flows.

I am wondering if there's any support to continue-on-fail? Something similar to recover in Scala futures? Basically handling an exception and allowing the chain to continue with a value or re-throwing the exception for the next failhandler to be triggered? I have use-cases where this is very handy.

https://www.scala-lang.org/api/2.12.3/scala/concurrent/Future.html#recover

thanks.

Naios commented 3 years ago

cti::recover and cti::rethrow are fully supported to change the current error state as described in the documentation. Those functions return a cti::result which can be returned from any handler to change the current error state, either recover from it or introduce an exceptional result:

http_request("example.com")
  .then([](std::string content) -> cti::result<int, int> {
    return recover(1, 2);
  })
  .fail([](cti::exception_t exception) -> cti::result<int, int> {
    return recover(1, 2);
  })
  .then([](int a, int b) -> cti::result<int, int> {
    // Recovered from the failure
  })
http_request("example.com")
  .then([](std::string content) {
    return rethrow(std::make_exception_ptr(std::exception{}));
  })
  .fail([](cti::exception_t exception) {
    return rethrow(std::make_exception_ptr(std::exception{}));
  })
  .next([](auto&&...) {
    return rethrow(std::make_exception_ptr(std::exception{}));
  });
noisypants commented 3 years ago

awesome! thanks. I missed that in the docs. I will give that a shot.