3 ms·
What is the difference between "Promise" and "Future"? From what I read, it is basically the same thing.
by runn1ng 11y ago
What is the difference between "Promise" and "Future"?
From what I read, it is basically the same thing.
- nanodeath 11y agoAs far as I can tell, there's two differences. 1) a Promise typically has a "public" API and a "private" API -- the private API has read/write access and the public API has only read access. This way you can return the public API object to consumers of a method/library without worrying about them mutating the promise. 2) Promises tend to be more...composable? Java's "CompletableFuture" API fails on the #1 point I made above, but otherwise has a whole slew of then* methods that aren't on Java's Future. Then again, that might just be because Java's Future is mind-numbingly simplistic.
- the_af 11y agoThen again, Scala's Future/Promise terminology seems to be something else, though of course related.
- btilly 11y agoPeople do not use the terms consistently. The difference is SUPPOSED to be that a Future is read-only while a Promise is settable. And should be set at most once! So you can use a Promise as a Future, but you can't always use a Future as a Promise. Linguistically you can understand this as, "I Promise you'll see a Future answer." I need to be able to write to the Promise to fulfill it. You can only hope that some day the Future will arrive.
- inglor 11y agoThat changes by language - what you say is true in Scala, but in JavaScript the terminology is different. A future is not defined, a promise is a future and a deferred is a promise.
- btilly 11y agoThat is what I mean about people being inconsistent. The ideas are much older than Scala, JavaScript, and many of the people commenting in this discussion. Believe it or not, both terms date back to the late 1970s. Scala got it right. JavaScript got it wrong. And polyglots like me have to suffer with having to sort out who means what, where, on what platform.
- dustingetz 11y agoIn Javascript: Promise is read-only (public interface), Deferred is writable (private) on JVM: Future is read-only (public), Promise is writable (private) The writable interface has "resolve" and "reject" to return a value. The readable interface has "then", "error", "finally" etc which let you compose and combine functions that deal in asynchronous values. The best Javascript implementation of promises is Bluebird, which doesn't expose a Deferred instance to avoid confusion, it is very nice. bluebirdjs.com/docs/api-reference.html