(fail promise exception)

204 views
Skip to first unread message

William la Forge

unread,
Aug 7, 2015, 3:14:30 PM8/7/15
to Clojure
A future fails when it throws an exception. How to do that with a future?

It looks like (fail future exception) does not do the trick: http://dev.clojure.org/display/design/Promises

Francis Avila

unread,
Aug 7, 2015, 4:52:47 PM8/7/15
to Clojure
Futures automatically capture exceptions raised in their bodies and reraise them when the future is derefed. Promises also throw exceptions when derefed.

Unlike promises, futures are created with the code that delivers their value, so calling fail and deliver explicitly on a future makes no sense.

Think of futures as a thin wrapper around a promise which spawns a thread, runs the code, and to the wrapped promise either delivers the result of the code or calls (fail private-promise raised-exception) for you.

William la Forge

unread,
Aug 7, 2015, 5:09:39 PM8/7/15
to Clojure
Sorry, I meant to ask how to fail with a promise? It seems that there is no fail method.

Francis Avila

unread,
Aug 7, 2015, 5:24:24 PM8/7/15
to Clojure
Huh, I was sure I had done this before, but I misremembered, I was using my own promise that rethrew Throwable instances on deref (and it was in clojurescript!)

Clojure promises have no notion of failure, only realized/not-realized. You need to deliver a sentinel type or value and check for it on deref, or implement your own promise type which does it for you. (It's not that hard: https://github.com/clojure/clojure/blob/clojure-1.7.0/src/clj/clojure/core.clj#L6803 )

William la Forge

unread,
Aug 7, 2015, 5:44:53 PM8/7/15
to Clojure
Still a newbie here, about to release my first Clojure project. But that clojure blob will take some digging on my part. I use a gummed up dereference function that checks for Throwable in the meantime. 

My goal right now is to become a Dtomic freelancer. So much to learn! Which is why I'm doing projects. Only way I know to become proficient! So yeah, I need to get to the point where I can roll my own. Similarly, my first project builds on the send method, but I really need to get to the point where I can roll my own agent.
 
In any case, thanks Francis. I can only promise more dumb questions in the future. ;-)

Atamert Ölçgen

unread,
Aug 8, 2015, 5:50:48 AM8/8/15
to clo...@googlegroups.com
Hi William,

Manifold deferreds can capture exceptions like future does.




--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Kind Regards,
Atamert Ölçgen

◻◼◻
◻◻◼
◼◼◼

www.muhuk.com

William la Forge

unread,
Aug 8, 2015, 9:31:13 PM8/8/15
to Clojure
Pretty neat stuff! I'm sure you had as much fun with it as I have with agent2: https://github.com/laforge49/agent2#readme

William la Forge

unread,
Aug 9, 2015, 3:29:25 PM8/9/15
to Clojure
Agent2 release 0.2.1 now wraps exceptions passed as values in a vector marked with the metadata flag :wrapped. The request-call method can now distinguish between exceptions thrown by a request (and rethrows them) and exceptions used as responses (and unwraps them before returning them). https://github.com/laforge49/agent2/releases

I appreciate your advice Frances. Just too much for me to deal with all at one time, this being my first release to Clojars and all.
Reply all
Reply to author
Forward
0 new messages