Whatever message this page gives is out now! Go check it out!
asyncAllOf(futures)| Parameter | Description |
|---|---|
| futures | Required. An array of Future objects. Each element must be a Future. |
<cfscript>
f1 = runAsync(function() { return 10; });
f2 = runAsync(function() { return 20; });
f3 = runAsync(function() { return 30; });
allFuture = asyncAllOf([f1, f2, f3]);
allFuture.get(); // wait for all
sum = f1.get() + f2.get() + f3.get();
writeOutput(sum); // 60
</cfscript>
<cfscript>
// asyncAllOf() - Wait for all futures to completewriteOutput("<h3>asyncAllOf() Demo</h3>");
writeOutput("<hr>");
// Create parallel futures
future1 = runAsync(() => {
local.threadName = createObject("java", "java.lang.Thread").currentThread().getName();
sleep(300);
return { data: "Product catalog", thread: local.threadName, time: 300 };
});
future2 = runAsync(() => {
local.threadName = createObject("java", "java.lang.Thread").currentThread().getName();
sleep(200);
return { data: "User preferences", thread: local.threadName, time: 200 };
});
startTime = getTickCount();
// wait for both together
allDone = asyncAllOf([future1, future2]);
// then collect results after both finish
result1 = future1.get();
result2 = future2.get();
elapsed = getTickCount() - startTime;
writeOutput("Future 1: " & result1.data & " on " & result1.thread & "<br>");
writeOutput("Future 2: " & result2.data & " on " & result2.thread & "<br>");
writeOutput("Total time: " & elapsed & "ms (parallel)<br>");
</cfscript>