Question

I'm really glad that EcmaScript 6 has promises built into the language, but the syntax will still be quite wordy:

new Promise(f).then(r => {
...
}).then(r2 => 
...
}).catch(...

I'm just wondering if there are any languages that have syntactic sugar for promises, so the result looks more like conventional imperative programming. Maybe hypothetically something like:

fetch f {
get r
  ...
get r2
  ...
catch
  ...
}
Was it helpful?

Solution

Yes, there are several such languages:

  • C# has async methods and the await keyword. Technically speaking, any object which implements a certain syntactic pattern can be awaited, but the intention is for this syntax to be used for, well, asynchronous programming, and the only objects in the framework which implement the syntactic pattern are Task<T> objects, which are futures/promises (depending on your exact definition of "future" and "promise", they may be futures, promises, both, or neither).
  • Visual Basic.NET has the same syntax with the same semantics
  • F# has asynchronous workflows, which were the inspiration for async/await
  • In languages with macros, libraries can implement their own syntax (within limits), and so there are promise libraries in Scala, Scheme, Common Lisp, Clojure, and other Lisps, which have what looks like builtin syntax.
  • Python 3.5+ has async / await
  • The next version of ECMAScript will very likely have async/await
  • The next version of C++ may or may not have resumable/await
  • And last but very important: Oz does not have special syntax for promises, because in Oz every variable is a dataflow-variable, and every value is a promise!
Licensed under: CC-BY-SA with attribution
scroll top