Note that there are some explanatory texts on larger screens.

plurals
  1. POAsync Futures running in sequence to completion
    text
    copied!<p>I encountered the following example (Example 1 below) of Futures which caused me to wonder if I could alter the way that I was handling Futures and remove all of the nested function calls that preserve order of processing, which however result in indentation which I find a bit messy.</p> <p>The altered version of my program did not work however. It did not preserve the order of processing and did not “wait” for function to complete. For example, before returning from the first call (fGetUserInput), another subsequent function was called.</p> <p>Why is it that in Example 1, all of the “1st level” “new Future”s processed sequentially, however in Example 2, my altered code, the order of processing is not preserved. While the call to fGetUserInput is being processed, one of the Futures that follows it is processed?</p> <p>Is it perhaps that “Example 1” only “works” because all of the statements are synchronous?</p> <p>I came across a reference to “runAsync”. Can that be used to achieve what I want? (process in sequence without all of the indentation).</p> <pre class="lang-dart prettyprint-override"><code> // Example 1. Code that I encountered for Futures // import 'dart:async'; main() { new Future(() =&gt; print('1')) .then((_) =&gt; print('a')) .then((_) =&gt; print('b')); new Future(() =&gt; print('2')) .then((_) =&gt; print('c')) .then((_) =&gt; print('d')); new Future(() =&gt; print('3')) .then((_) =&gt; new Future(() =&gt; print('e')) .then((_) =&gt; print('f')) ); new Future(() =&gt; print('4')) .then((_) =&gt; new Future(() =&gt; print('g')) .then((_) =&gt; print('d')) ); } </code></pre> <p>The above results in the following console output order :-</p> <pre> 1 a b 2 c d 3 4 e f g d </pre> <p>Which I thought made sense.</p> <p>Therefore, I modified my code to test it as follows :-</p> <pre class="lang-dart prettyprint-override"><code> // Example 2. Altered version of my code which // // does not preserve the order of processing, // // which is necessary for program to function. // new async.Future(() =&gt; fGetUserInput()) .then((lInput) { iMaxIters = int.parse(lInput[4]); tClearTable = (lInput[5] == "y"); iDivisor = fInitialize(iMaxIters); tgPrint = false; // printing off sUri = "postgres://${lInput[1]}:${lInput[2]}@localhost:5432/${lInput[3]}"; sStartTime = lInput[7]; }) .catchError((oError) =&gt; fFatal("Get User Input", oError)); new async.Future(() =&gt; fConnectToDb(sUri, sStartTime)) .then((bool tConnected) { if (ogDb == null) fFatal("Unable to connect to database", ""); print ("Processing database ......"); }) .catchError((oError) =&gt; fFatal("Connect to Db", oError)); new async.Future(() =&gt; fClearTable(tClearTable)) .then((sResult) =&gt; print (sResult+"\n")) .catchError((oError) =&gt; fFatal("Clear Table", oError)); new async.Future(() =&gt; fProcessInserts(iMaxIters, iDivisor)) .then((sResult) =&gt; print ("")) .catchError((oError) =&gt; fFatal("Process Inserts", oError)); new async.Future(() =&gt; fSetupRandKeys()) .then((sResult) =&gt; print ("")) .catchError((oError) =&gt; fFatal("Setup Random Keys", oError)); new async.Future(() =&gt; fProcessUpdates(iMaxIters, iDivisor)) .then((sResult) { String sTotValue = fFormatAmount(igGrandTotAmt, true, 2); fPrint ("Grand Total added to database = \$${sTotValue}"); ogDb.close(); exit(0); }) .catchError((oError) =&gt; fFatal("Process Updates", oError)); } void fFatal (String sMessage, Error oError) { print("\n\nFatal Error. $sMessage\n${oError}"); exit(1); } async.Future&lt;String&gt; fProcessInserts(int iMaxIters, int iDiv) { async.Completer oCompleter = new async.Completer&lt;String&gt;(); int iTot = 0; Function fLoop; print ("\nProcessing Inserts ......"); fResetAndStartWatch(); </code></pre> <p>The following is my code prior to the above changes, and the following Example 3 appears to work OK. I don't like the extent of indentation, and in situations with more function calls, that would increase the extent of indentation. I was hoping for a more elegant way to do it.</p> <pre class="lang-dart prettyprint-override"><code> // Example 3: The original version of my code // // which does preserve the order of processing // void main() { print(""); String sCheckPoint = "Get User Input"; fGetUserInput() .then((lInput) { int iMaxIters = int.parse(lInput[4]); bool tClearTable = (lInput[5] == "y"); int iDiv = fInitialize(iMaxIters); tgPrint = false; // printing off String sUri = "postgres://${lInput[1]}:${lInput[2]}@localhost:5432/${lInput[3]}"; sCheckPoint = "Connect to Database"; fConnectToDb(sUri, lInput[7]).then((bool tConnected) { if (ogDb == null) fFatal(sCheckPoint, "Unable to conenct to Db"); print ("Processing database ......"); sCheckPoint = "Clear Table"; fClearTable(tClearTable).then((sResult) { print (sResult+"\n"); sCheckPoint = "Process Inserts"; fProcessInserts(iMaxIters, iDiv).then((sResult) { print; sCheckPoint = "Set-up Random Keys"; fSetupRandKeys().then((sResult) { print; sCheckPoint = "Process Updates"; fProcessUpdates(iMaxIters, iDiv).then((sResult) { String sTotValue = fFormatAmount(igGrandTotAmt, true, 2); fPrint ("Grand Total added to database = \$${sTotValue}"); ogDb.close(); exit(0); }); }); }); }); }); }) .catchError((oError) =&gt; fFatal(sCheckPoint, oError)); } void fFatal (String sMessage, Error oError) { print("\n\nFatal Error. $sMessage\n${oError}"); exit(1); } async.Future&lt;String&gt; fProcessInserts(int iMaxIters, int iDiv) { async.Completer oCompleter = new async.Completer&lt;String&gt;(); int iTot = 0; Function fLoop; print ("Processing Inserts ......"); fResetAndStartWatch(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload