Testing For Future Data-Types With isFuture() And isEmptyFuture() In ColdFusion 2018
ColdFusion 2018 introduced us to the concept of Futures. Futures, like Promises, provide hooks into the eventual resolution of data. Unfortunately, ColdFusion 2018 doesn't seem to have any "decision functions" geared towards Future detection. Luckily, the isInstanceOf() function provides us with an easy stop-gap solution (assuming these functions will be added to the core language eventually). By using the isInstanceOf() function we can take any variable and check to see if it references a Future or an EmptyFuture.
Under the hood, Futures and EmptyFutures are just custom Java classes provided by the ColdFusion runtime:
- coldfusion.runtime.async.Future
- coldfusion.runtime.async.EmptyFuture
As such, we can use the isInstanceOf() function to see if a given ColdFusion variable is actually a reference to an instance of one of these custom Java classes:
<cfscript>
/**
* I determine if the given value is a ColdFusion Future.
*
* @value I am the value being tested.
* @output false
*/
public boolean function isFuture( required any value ) {
return( isInstanceOf( value, "coldfusion.runtime.async.Future" ) );
}
/**
* I determine if the given value is a ColdFusion EmptyFuture.
*
* @value I am the value being tested.
* @output false
*/
public boolean function isEmptyFuture( required any value ) {
return( isInstanceOf( value, "coldfusion.runtime.async.EmptyFuture" ) );
}
// ------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------- //
future = runAsync(
function() {
return( "value" );
}
);
emptyFuture = runAsync();
// Test the future data type.
writeOutput( "isFuture( future ) : #isFuture( future )# <br />" );
writeOutput( "isEmptyFuture( future ) : #isEmptyFuture( future )# <br />" );
// Test the empty future data type.
writeOutput( "isFuture( emptyFuture ) : #isFuture( emptyFuture )# <br />" );
writeOutput( "isEmptyFuture( emptyFuture ) : #isEmptyFuture( emptyFuture )# <br />" );
</cfscript>
In this demo, we're creating both a normal Future object and an EmptyFuture object. We're then using our custom isFuture() and isEmptyFuture() functions to inspect the two objects. And, when we run this code, we get the following ColdFusion output:
isFuture( future ) : YES
isEmptyFuture( future ) : NOisFuture( emptyFuture ) : NO
isEmptyFuture( emptyFuture ) : YES
As you can see, our isFuture() and isEmptyFuture() user defined functions (UDFs) were able to detect Futures and EmptyFutures, respectively.
Having this kind of functionality can make more complex Future-handling code possible (such as automatically "unwrapping" Futures when they are the result of an operation). However, be aware that when you save a Future to an intermediary variable, ColdFusion does bad things.
Want to use code from this post? Check out the license.
Reader Comments
Hi Ben,
Greetings and Good News!!
We have done some recent changes to ColdFusion 2018 Async Framework - the first level then(), error() and their corresponding timed versions are unblocking now. It should be available as a part of ColdFusion 2018 update.
I will keep you posted on the update details.