Skip to content

Commit

Permalink
(deferred_promise) simplify #get
Browse files Browse the repository at this point in the history
  • Loading branch information
stakach committed Feb 10, 2019
1 parent cd91511 commit d593772
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 18 deletions.
20 changes: 4 additions & 16 deletions src/promise/deferred_promise.cr
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,10 @@ class Promise::DeferredPromise(Input) < Promise
end

# pause the current fiber and wait for the resolution to occur
def get
channel = Channel(Proc(Input)).new

spawn do
self.then(->(result : Input) {
channel.send(->{ result })
nil
}, ->(rejection : Exception) {
# We provide the type_var here as a hint to the compiler
# Most things work without it however Promise.all segfaults when not specified
channel.send(->{ raise rejection; self.type_var })
nil
})
end

channel.receive.call
def get : Input
result = raw_value
raise result if result.is_a?(Exception)
result
end

def raw_value
Expand Down
2 changes: 1 addition & 1 deletion src/promise/rejected_promise.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Promise::RejectedPromise(Input) < Promise::DeferredPromise(Input)
super()
end

def get
def get : Input
raise @rejection
end

Expand Down
2 changes: 1 addition & 1 deletion src/promise/resolved_promise.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Promise::ResolvedPromise(Input) < Promise::DeferredPromise(Input)
end

# get the value directly if the promise is resolved
def get
def get : Input
@value
end

Expand Down

0 comments on commit d593772

Please sign in to comment.