Search code examples
promisecapnprotokj

Convert kj::Promise<kj::Exception> to kj::Promise<void>


I can return a kj::Exception where a kj::Promise is expected, like so:

kj::Promise<void> someFunc() {
  return KJ_EXCEPTION(FAILED, "some description");
}

But what if I end up having a kj::Promise<kj::Exception> in a place where I don't have a waitScope? For instance:

kj::Promise<void> someFunc(kj::Promise<void> somePromise) {
  return somePromise.then([]() {
    return KJ_EXCEPTION(FAILED, "some description");
  });
}

There my compiler complains that there is no valid conversion between kj::Promise<kj::Exception> and kj::Promise<void>.

Is there a way around that?


Solution

  • I'm a little surprised that I didn't prohibit Promise<Exception> in the first place; using such a construction likely leads to a lot of problems.

    You can avoid it in your code by explicitly declaring the return type of the lambda to be Promise<void>, i.e.:

      return somePromise.then([]() -> kj::Promise<void> {