Whatever message this page gives is out now! Go check it out!

whenComplete (Sync)

Last update:
May 18, 2026
Runs a callback on the completing thread when this Future finishes, whether it succeeds or fails. The callback observes the outcome; it does not replace a failed Future with a success.

Description

whenComplete is an instance method on a CFML Future. When this stage completes (normal completion or exceptional completion), your action runs on the completing thread. The action receives two arguments: the result value (or null if the stage failed) and the error (or null if the stage succeeded). Use it for logging, metrics, or resource cleanup.
This is the synchronous counterpart to whenCompleteAsync. To fold success and failure into a single new value, use handle instead.

Returns

A new Future.

Category

Asynchronous programming

Function syntax

future.whenComplete(action)

Parameters

ParameterDescription
actionRequired. A function (value, error) that observes completion. One of the two arguments is null depending on success or failure.

Example

Record completion after an async map stage.

<cfscript>
    future = runAsync(function() { return 10; })
    .thenApplyAsync(function(x) { return x * 2; })
    .whenComplete(function(value, error) {
        variables.completed = true;
        variables.finalValue = value;
    });
    future.get();
    writeOutput(variables.completed & ":" & variables.finalValue); // true:20
</cfscript>
      

See also

Share this page

Was this page helpful?
We're glad. Tell us how this page helped.
We're sorry. Can you tell us what didn't work for you?
Thank you for your feedback. Your response will help improve this page.

On this page