Search code examples
coldfusioncoldfusion-2016

How to make ColdFusion wait?


Let's say I have a function called takesFiveSeconds(). I want to see the outcome after it's finished.

takesFiveSeconds()
writeDump(outcome);  // don't execute until takesFiveSeconds is finished
abort;

// the rest of the code runs //

How can I accomplish this? I understand CF doesn't have promises yet.


Here's what I've tried:

Thread

thread name="t1"
{ 
    takesFiveSeconds()
} 
thread name="t2"
{ 
    threadjoin("t1",1000); 
    writeDump(outcome);
    abort;
} 
threadjoin("t2"); 

I'm not sure if this works because the site finishes loading before it hits the abort, and I never see the writeDumps.


Sleep

takesFiveSeconds()
Sleep(15000);
writeDump(outcome);
abort;

All this does is take an extra long time to tell me outcome was dumped before takesFiveSeconds()` finished.


Solution

  • Here is the simple approach:

    <cfset start_time = now()>
    <cfloop condition="DateDiff('s', start_time, now()) LT 5">
         <cfset tmp = "wait">
    </cfloop>