Async programming; future and promises

h/t Sean Corfield in the Lucee Google Group
https://groups.google.com/d/topic/lucee/Y1i0DFclXOA/discussion

We’ve had thread in CFML for a long time now but it’s really only suitable for running procedural (side-effecting) code in background threads. It’s not really very good for async programming in general.

I think it would be a really nice enhancement to the Lucee dialect to fully support futures and promises.

There are several implementations out there in various languages so there are quite a few specific models to choose from.

For those not familiar, the general idea is something like this:

var a = future( some expression );
var b = future( some other expression );
var c = a.get() + b.get();

This spins off some expression and some other expression to execute in other threads, but immediately returns a reference to the “future” value. Calling get() on the future will block until the value is available (or return immediately if the expression has already completed). Calling get() multiple times will just return the computed value (it’s cached for the life of the reference).

Promises are similar but rely on other code to compute the value:

var a = promise();
var b = promise();
…
// maybe in another thread:
a.deliver( 42 );
…
// maybe in yet another thread:
b.deliver( 13 );
…
var c = a.get() + b.get();

Like futures, the get() call will block until the promise has a value.

Both of these allow complex operations — time-consuming ones — to be broken down and easily run concurrently while maintaining much clearer code than trying to manage a whole bunch of thread { … } statements assigning values back and forth.

Fancier features allow for completion callbacks, error callbacks, timeouts (and timeout callbacks), etc.

Sean Corfield – (904) 302-SEAN
An Architect’s View – http://corfield.org/

6 Likes

Tracking in JIRA at:

Add futures and promises to the Lucee dialect
https://luceeserver.atlassian.net/browse/LDEV-311

Just throwing this out there, I got bored so whipped up a Lucee 5 extension that brings in Javascript-style Promises over at GitHub - SimonHooker/lucee-promise: Javascript style promises dressed up as a Lucee 5 extension

2 Likes

Lucee now has support for runAsync

1 Like