Permalink
Browse files

Add a way to get broken promise cause.

Using .result will throw the exception if the promise was broken; this
provides a way for getting hold of it if needed.
  • Loading branch information...
1 parent 6edd19d commit 6297ac572c60b2b3570a4eef0c42f381298a9095 @jnthn jnthn committed Aug 9, 2013
Showing with 11 additions and 0 deletions.
  1. +11 −0 src/vm/jvm/core/Threading.pm
@@ -48,6 +48,9 @@ my class X::Promise::Combinator {
has $.combinator;
method message() { "Can only use $!combinator to combine other Promise objects" }
}
+my class X::Promise::CauseOnlyValidOnBroken {
+ method message() { "Can only call cause on a broken promise" }
+}
my class Promise {
has $.scheduler;
has $.status;
@@ -135,6 +138,14 @@ my class Promise {
so $!status == any(Failed, Completed)
}
+ method cause(Promise:D:) {
+ if $!status == Failed {
+ $!result
+ } else {
+ X::Promise::CauseOnlyValidOnFailed.new.throw
+ }
+ }
+
method then(Promise:D: &code) {
$!then_lock.lock();
if $!status == any(Failed, Completed) {

0 comments on commit 6297ac5

Please sign in to comment.