Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
Ben Nadel at cf.Objective() 2011 (Minneapolis, MN) with: Jason Long
Ben Nadel at cf.Objective() 2011 (Minneapolis, MN) with: Jason Long

Testing For Future Data-Types With isFuture() And isEmptyFuture() In ColdFusion 2018

By Ben Nadel on
Tags: ColdFusion

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 ) : NO

isFuture( 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.



Looking For A New Job?

Ooops, there are no jobs. Post one now for only $29 and own this real estate!

100% of job board revenue is donated to Kiva. Loans that change livesFind out more »

Reader Comments

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
NEW: Some basic markdown formatting is now supported: bold, italic, blockquotes, lists, fenced code-blocks. Read more about markdown syntax »
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.