Asynchronous programming

Description

In ACF18, introduce support for asynchronous programming via Future. A Future is an eventual result of an asynchronous operation.

Asynchronous programming is useful when you want to reduce the average response time of an application.

Example

getAccountBalance = function(){ var balance = 120000; return balance; } function payCreditCardBill(accountBalance){ var ccBill = 1890; return accountBalance-ccBill; } payEMIs = function(accountBalance){ var mortgageEMI = 1000; var carLeaseEMI = 750; var healthInsuranceEMI = 250; return accountBalance-(mortgageEMI+carLeaseEMI+healthInsuranceEMI); } miscellenousExpenses = function(accountBalance){ var shopping = 1500; var clubExpense =1000; var casinoExpense = 2000; return accountBalance-(shopping+clubExpense+casinoExpense); } checkBalance = function(accountBalance){ while(accountBalance > 5000){ accountBalance = miscellenousExpenses(accountBalance); writeOutput("checkBalance = " & accountBalance & "<br/>"); } if(accountBalance < 5000) throw (message="Account balance below threshold!!!", type="info"); } errorHandler = function(error){ if(error.message contains "Account balance below threshold!"){ return "You have reached your spending limit!"; } } future = runAsync(getAccountBalance).then(payCreditCardBill).then(payEMIs).then(miscellenousExpenses).then(checkBalance).error(errorHandler); writeOutput(future.get());

Result: Example1_Result.PNG

Methods available with runAsync are:

cancel(); error(callback, timeout); error(callback); get(); get(timeout); isCancelled(); isDone(); then(callback); then(callback, timeout);

UDF Method

UDF Method would be either a closure reference, closure, or a User Defined Method

<cfscript> function add(){ return 10+20; } Future = runAsync(add); writeDump(Future.isDone());// Yes writeDump(Future.get()); //30 writeDump(Future.isCancelled()); //no </cfscript>

Empty future

An empty future is an object, which can be explicitly marked as complete with a result value. It can be used in producer/consumer scenarios.

p = runAsync(); // empty future p.complete(10); writeOutput(p.get()); //10

The methods available on an empty Future are:

cancel() get() isCancelled() isDone() complete(value)

Attachments

1

Activity

Chris Dillon 
19 April 2019 at 22:06

Zac Spitzer 
16 April 2019 at 22:04

can you post a new bug and link it?

Chris Dillon 
16 April 2019 at 15:16

From my tests, runasync does not run multiple gets at the same time.

Pothys - MitrahSoft 
2 May 2018 at 08:53

Hi ,

I've added test case for this ticket.

Pull Request: https://github.com/lucee/Lucee/pull/478

Fixed

Details

Assignee

Reporter

New Issue warning screen

Before you create a new Issue, please post to the mailing list first https://dev.lucee.org

Once the issue has been verified, one of the Lucee team will ask you to file an issue

Fix versions

Priority

Created 30 April 2018 at 15:03
Updated 17 February 2023 at 03:02
Resolved 4 May 2018 at 20:52