<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.visual-prolog.com/index.php?action=history&amp;feed=atom&amp;title=WebSocket_futures</id>
	<title>WebSocket futures - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.visual-prolog.com/index.php?action=history&amp;feed=atom&amp;title=WebSocket_futures"/>
	<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=WebSocket_futures&amp;action=history"/>
	<updated>2026-04-12T01:34:01Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.37.1</generator>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=WebSocket_futures&amp;diff=4445&amp;oldid=prev</id>
		<title>Thomas Linder Puls: grammar</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=WebSocket_futures&amp;diff=4445&amp;oldid=prev"/>
		<updated>2017-08-20T18:52:19Z</updated>

		<summary type="html">&lt;p&gt;grammar&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 20:52, 20 August 2017&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;webSocket library for use in a web-server is asynchronous and here I will &amp;quot;touch the surface&amp;quot; of programming asynchronous using futures.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;The &amp;lt;vp&amp;gt;&lt;/ins&gt;webSocket&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/vp&amp;gt; &lt;/ins&gt;library for use in a web-server is asynchronous and here I will &amp;quot;touch the surface&amp;quot; of programming asynchronous using futures.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;A webSocket service has a single &amp;lt;vp&amp;gt;onAttach_async&amp;lt;/vp&amp;gt; predicate&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;A webSocket service has a single &amp;lt;vp&amp;gt;onAttach_async&amp;lt;/vp&amp;gt; predicate&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Thomas Linder Puls</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=WebSocket_futures&amp;diff=4444&amp;oldid=prev</id>
		<title>Thomas Linder Puls: init</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=WebSocket_futures&amp;diff=4444&amp;oldid=prev"/>
		<updated>2017-08-20T18:44:27Z</updated>

		<summary type="html">&lt;p&gt;init&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;webSocket library for use in a web-server is asynchronous and here I will &amp;quot;touch the surface&amp;quot; of programming asynchronous using futures.&lt;br /&gt;
&lt;br /&gt;
A webSocket service has a single &amp;lt;vp&amp;gt;onAttach_async&amp;lt;/vp&amp;gt; predicate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
interface webSocketService&lt;br /&gt;
    open core, pfc\asynchronous\&lt;br /&gt;
&lt;br /&gt;
predicates&lt;br /&gt;
    onAttach_async : (httpRequest Request, webSocket WebSocket) -&amp;gt; future{unit}.&lt;br /&gt;
    % @short This predicate is invoked when a client created a webSocket connection to this service.&lt;br /&gt;
    % The service is registered in a requestQueue_webSocket.&lt;br /&gt;
    % @end&lt;br /&gt;
&lt;br /&gt;
end interface webSocketService&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You create a request queue from your service:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
requestQueue_webSocket::new(threadpool, demoWebSocketService::new())&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which you attach to an urlGroup like you do with the other kinds of services.&lt;br /&gt;
&lt;br /&gt;
The overall scenario is the following:&lt;br /&gt;
&lt;br /&gt;
* Front end code creates a webSocket connection to your service&lt;br /&gt;
* If successful your onAttach_async  predicate will be called with the request that made the connection and with the created &amp;quot;server side end&amp;quot; of the web socket.&lt;br /&gt;
* The request can be used to provide additional information about the connection (who, why, what about, …)&lt;br /&gt;
* The front end and your service can then receive and send messages on the web socket, until one of the ends closes the web socket.&lt;br /&gt;
&lt;br /&gt;
Front-end code can look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;JavaScript&amp;quot;&amp;gt;&lt;br /&gt;
function wsEcho() {&lt;br /&gt;
    const ws = new WebSocket(wsUrl + &amp;#039;echo&amp;#039;)&lt;br /&gt;
    const id = getId()&lt;br /&gt;
    ws.onopen = evt =&amp;gt; {&lt;br /&gt;
        logP(&amp;#039;webSocket connected &amp;#039; + id)&lt;br /&gt;
        ws.send(&amp;#039;Hello Visual Prolog User! (echo)&amp;#039; + id)&lt;br /&gt;
    }&lt;br /&gt;
    ws.onclose = evt =&amp;gt; logP(&amp;#039;webSocket diconnected &amp;#039; + id)&lt;br /&gt;
    ws.onerror = evt =&amp;gt; logP(&amp;#039;&amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;Error &amp;#039; + id + &amp;#039;:&amp;lt;/span&amp;gt; &amp;#039; + evt.data)&lt;br /&gt;
    ws.onmessage = evt =&amp;gt; {&lt;br /&gt;
        logP(&amp;#039;&amp;lt;span style=&amp;quot;color: blue&amp;quot;&amp;gt;Response &amp;#039; + id + &amp;#039;: &amp;#039; + evt.data + &amp;#039;&amp;lt;/span&amp;gt;&amp;#039;)&lt;br /&gt;
        ws.close()&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The front-end code have callbacks attached to deal with the four events &amp;#039;&amp;#039;&amp;#039;open&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;message&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;error&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;close&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
I will not go into details about the code: I am certain that any front-end programmer will use some kind of library software on top of the web sockets anyway.&lt;br /&gt;
&lt;br /&gt;
Notice however that a web socket URL looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ws://&amp;lt;server&amp;gt;/&amp;lt;path&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the &amp;#039;&amp;#039;&amp;#039;webServiceDemo&amp;#039;&amp;#039;&amp;#039; example the last part of the URL is used to distinguish between different webSocket services/behaviors:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    onAttach_async(Request, WebSocket) = onAttach2_async(filename::getLastDirectory(Request:path, _), WebSocket).&lt;br /&gt;
&lt;br /&gt;
predicates&lt;br /&gt;
    onAttach2_async : (string Last, webSocket WebSocket) -&amp;gt; future{unit}.&lt;br /&gt;
clauses&lt;br /&gt;
    onAttach2_async(&amp;quot;echo&amp;quot;, WebSocket) = echoLoop_async(WebSocket) :-&lt;br /&gt;
        !.&lt;br /&gt;
&lt;br /&gt;
    onAttach2_async(&amp;quot;async&amp;quot;, WebSocket) = async_async(WebSocket) :-&lt;br /&gt;
        !.&lt;br /&gt;
&lt;br /&gt;
    onAttach2_async(&amp;quot;jsonAsync&amp;quot;, WebSocket) = jsonAsync_async(WebSocket) :-&lt;br /&gt;
        !.&lt;br /&gt;
&lt;br /&gt;
    onAttach2_async(&amp;quot;ball&amp;quot;, WebSocket) = ball_async(WebSocket) :-&lt;br /&gt;
        !.&lt;br /&gt;
&lt;br /&gt;
    onAttach2_async(Service, WebSocket) = &lt;br /&gt;
        WebSocket:writeMessage_async(webSocket::close(210, string::format(&amp;quot;Unknown service %&amp;quot;, Service))).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You have of course noticed that the predicates ends in &amp;lt;vp&amp;gt;_async&amp;lt;/vp&amp;gt; and that they are functions which returns a &amp;lt;vp&amp;gt;future{unit}&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
This is because they are asynchronous predicates based on our promise/future library.&lt;br /&gt;
&lt;br /&gt;
Let us first look at some (illegal) pseudo code (inspired by the asynchronous features of C# and JavaScript (ES2017)):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
    echoLoop_async : (webSocket WebSocket) asynchronous.&lt;br /&gt;
clauses&lt;br /&gt;
    echoLoop_async(WebSocket) :-&lt;br /&gt;
        Message = await WebSocket:readMessage_async(),&lt;br /&gt;
        if webSocket::close(_, _) = Message then&lt;br /&gt;
        else&lt;br /&gt;
            await WebSocket:writeMessage_async(Message),&lt;br /&gt;
            echoLoop_async(WebSocket)&lt;br /&gt;
        end if.&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you delete the words &amp;lt;vp&amp;gt;asynchronous&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;await&amp;lt;/vp&amp;gt; the code looks like a completely normal (synchronous) loop.  The &amp;lt;vp&amp;gt;await&amp;lt;/vp&amp;gt; indicates that we await the completion of an asynchronous computation before we continue.  However we do not actually wait in that point, instead we suspend the computation until the asynchronous computation completes and then we resume the computation again from the await point.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vp&amp;gt;await&amp;lt;/vp&amp;gt; resembles a &amp;lt;vp&amp;gt;vpi::processEvents&amp;lt;/vp&amp;gt; in the sense that it is an explicit point where other computations can take place.&lt;br /&gt;
&lt;br /&gt;
The actual running of this code is however quite different, and has more resemblance to our &amp;lt;vp&amp;gt;nondeterm&amp;lt;/vp&amp;gt; predicates.  When we call the asynchronous predicate the execution will continue normally until we meet an &amp;lt;vp&amp;gt;await&amp;lt;/vp&amp;gt;; at that point it will return to the caller.  Later when the &amp;lt;vp&amp;gt;await&amp;lt;/vp&amp;gt;&amp;#039;ed operation completes we will jump back into the code (just like we jump back into &amp;lt;vp&amp;gt;nondeterm&amp;lt;/vp&amp;gt; code on failure) and continue the execution, until we meet another &amp;lt;vp&amp;gt;await&amp;lt;/vp&amp;gt; or finish.  But &amp;lt;vp&amp;gt;nondeterm&amp;lt;/vp&amp;gt; predicates we only return from an asynchronous predicate the first time, the other jump-back-into the code will never return to the original call point again.&lt;br /&gt;
&lt;br /&gt;
I.e. to the caller an asynchronous predicate completes/returns when the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;first&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; &amp;lt;vp&amp;gt;await&amp;lt;/vp&amp;gt; is met.  The other parts of the code are run when the relevant asynchronous computations complete.&lt;br /&gt;
&lt;br /&gt;
Presumably the caller of an asynchronous predicate is interested in the final result of the computation and have little interest in the &amp;quot;random&amp;quot; state after the first &amp;lt;vp&amp;gt;await&amp;lt;/vp&amp;gt;. Therefore the caller actually receives a &amp;lt;vp&amp;gt;future{…}&amp;lt;/vp&amp;gt; object that represents the result of the entire asynchronous computation.&lt;br /&gt;
&lt;br /&gt;
Unfortunately, &amp;lt;vp&amp;gt;asynchronous&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;await&amp;lt;/vp&amp;gt; are not implemented in our language, so you will have to &amp;quot;translate&amp;quot; the code yourself.  Hence you will need to know more about &amp;lt;vp&amp;gt;future&amp;lt;/vp&amp;gt;&amp;#039;s.&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;vp&amp;gt;future&amp;lt;/vp&amp;gt; represents the result of an asynchronous computation that may not complete until some point in the future. As such it has three major states:&lt;br /&gt;
&lt;br /&gt;
* it will be &amp;#039;&amp;#039;&amp;#039;pending&amp;#039;&amp;#039;&amp;#039; until the computation is completed (or cancelled)&lt;br /&gt;
* it will be &amp;#039;&amp;#039;&amp;#039;completed&amp;#039;&amp;#039;&amp;#039; once the computation has finished. The completion can happen with:&lt;br /&gt;
** a &amp;#039;&amp;#039;&amp;#039;success&amp;#039;&amp;#039;&amp;#039; indicating that the computation went well&lt;br /&gt;
** or an &amp;#039;&amp;#039;&amp;#039;error&amp;#039;&amp;#039;&amp;#039; indicating that something went wrong (an exception was raised and not caught)&lt;br /&gt;
* it can also be &amp;#039;&amp;#039;&amp;#039;cancelled&amp;#039;&amp;#039;&amp;#039; meaning that the computation was cancelled before it completed&lt;br /&gt;
&lt;br /&gt;
There are only two possible state transitions:&lt;br /&gt;
&lt;br /&gt;
* A &amp;#039;&amp;#039;&amp;#039;pending&amp;#039;&amp;#039;&amp;#039; future can &amp;#039;&amp;#039;&amp;#039;complete&amp;#039;&amp;#039;&amp;#039; changing to the &amp;#039;&amp;#039;&amp;#039;completed&amp;#039;&amp;#039;&amp;#039; state&lt;br /&gt;
* A &amp;#039;&amp;#039;&amp;#039;pending&amp;#039;&amp;#039;&amp;#039; future can &amp;#039;&amp;#039;&amp;#039;cancel&amp;#039;&amp;#039;&amp;#039; changing to the &amp;#039;&amp;#039;&amp;#039;cancelled&amp;#039;&amp;#039;&amp;#039; state&lt;br /&gt;
&lt;br /&gt;
There are many ways to use future&amp;#039;s.  Here we will focus on how to &amp;quot;translate&amp;quot; asynchronous predicates into ordinary code using the &amp;lt;vp&amp;gt;thenDo&amp;lt;/vp&amp;gt; operation on &amp;lt;vp&amp;gt;futures&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vp&amp;gt;thenDo&amp;lt;/vp&amp;gt; is used to handle the &amp;#039;&amp;#039;&amp;#039;complete&amp;#039;&amp;#039;&amp;#039; state transition as a kind of event listening (it will also automatically handle the &amp;#039;&amp;#039;&amp;#039;cancel&amp;#039;&amp;#039;&amp;#039; state transition).  It is however more complex than normal event listening, because the result of the event handling is itself an asynchronous operation.&lt;br /&gt;
&lt;br /&gt;
An asynchronous function that returns a value of type &amp;lt;vp&amp;gt;Type&amp;lt;/vp&amp;gt; translates into a function that returns &amp;lt;vp&amp;gt;future{Type}&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If the predicate does not return anything then it is treated as if is a function that returns &amp;lt;vp&amp;gt;core::unit&amp;lt;/vp&amp;gt;, and thus translates into a function that returns &amp;lt;vp&amp;gt;future{unit}&amp;lt;/vp&amp;gt;.  &amp;lt;vp&amp;gt;core::unit&amp;lt;/vp&amp;gt; is a domain that only contains the value &amp;lt;vp&amp;gt;core::unit&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
So &lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
    echoLoop_async : (webSocket WebSocket) asynchronous.&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
translates into &lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
    echoLoop_async : (webSocket WebSocket) -&amp;gt; future{unit}.&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Likewise the readMessage_async function in:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
        Message = await WebSocket:readMessage_async(),&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Is actually function that returns &amp;lt;vp&amp;gt;future{message Message}&amp;lt;/vp&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
predicates&lt;br /&gt;
    readMessage_async : () -&amp;gt; future{message Message}.&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;vp&amp;gt;await&amp;lt;/vp&amp;gt; will translate into a &amp;lt;vp&amp;gt;thenDo&amp;lt;/vp&amp;gt; call on the returned &amp;lt;vp&amp;gt;future&amp;lt;/vp&amp;gt;; it is called with a &amp;#039;&amp;#039;continuation&amp;#039;&amp;#039; that receives the &amp;lt;vp&amp;gt;Message&amp;lt;/vp&amp;gt; we await and returns a &amp;lt;vp&amp;gt;future&amp;lt;/vp&amp;gt; &amp;lt;vp&amp;gt;F1&amp;lt;/vp&amp;gt; that represents the remaining operation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    echoLoop_async(WebSocket) =&lt;br /&gt;
        WebSocket:readMessage_async():thenDo(&lt;br /&gt;
            { (Message) = F1 :-&lt;br /&gt;
&amp;lt;&amp;lt;&lt;br /&gt;
                if webSocket::close(_, _) = Message then&lt;br /&gt;
                else&lt;br /&gt;
                    await WebSocket:writeMessage_async(Message),&lt;br /&gt;
                    echoLoop_async(WebSocket)&lt;br /&gt;
                end if&lt;br /&gt;
&amp;gt;&amp;gt;&lt;br /&gt;
            }).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the then-part of the if-statement we must return a &amp;lt;vp&amp;gt;future{unit}&amp;lt;/vp&amp;gt; that represent the completion of the completion of the entire operation.  In that point the entire operation is completed so we create a &amp;lt;vp&amp;gt;future{unit}&amp;lt;/vp&amp;gt; that is already completed.  This is done with &amp;lt;vp&amp;gt;future::newUnit()&amp;lt;/vp&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    echoLoop_async(WebSocket) =&lt;br /&gt;
        WebSocket:readMessage_async():thenDo(&lt;br /&gt;
            { (Message) = F1 :-&lt;br /&gt;
                if webSocket::close(_, _) = Message then&lt;br /&gt;
                    F1 = future::newUnit()&lt;br /&gt;
                else&lt;br /&gt;
&amp;lt;&amp;lt;&lt;br /&gt;
                    await WebSocket:writeMessage_async(Message),&lt;br /&gt;
                    echoLoop_async(WebSocket)&lt;br /&gt;
&amp;gt;&amp;gt;&lt;br /&gt;
                end if&lt;br /&gt;
            }).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the else-part we have a (&amp;lt;vp&amp;gt;future{unit}&amp;lt;/vp&amp;gt;) &amp;lt;vp&amp;gt;await&amp;lt;/vp&amp;gt; on &amp;lt;vp&amp;gt;writeMessage_async&amp;lt;/vp&amp;gt;. This translates into a &amp;lt;vp&amp;gt;thenDo&amp;lt;/vp&amp;gt;-call with a continuation that receives the (&amp;lt;vp&amp;gt;core::unit&amp;lt;/vp&amp;gt;) result of the &amp;lt;vp&amp;gt;writeMessage_async&amp;lt;/vp&amp;gt; call and must return a &amp;lt;vp&amp;gt;future&amp;lt;/vp&amp;gt; &amp;lt;vp&amp;gt;F2&amp;lt;/vp&amp;gt; that represents the remaining operation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    echoLoop_async(WebSocket) =&lt;br /&gt;
        WebSocket:readMessage_async():thenDo(&lt;br /&gt;
            { (Message) = F1 :-&lt;br /&gt;
                if webSocket::close(_, _) = Message then&lt;br /&gt;
                    F1 = future::newUnit()&lt;br /&gt;
                else&lt;br /&gt;
                    F1 =&lt;br /&gt;
                        WebSocket:writeMessage_async(Message):thenDo(&lt;br /&gt;
                            { = F2 :-&lt;br /&gt;
&amp;lt;&amp;lt;&lt;br /&gt;
                                echoLoop_async(WebSocket)&lt;br /&gt;
&amp;gt;&amp;gt;&lt;br /&gt;
                            })&lt;br /&gt;
                end if&lt;br /&gt;
            }).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;vp&amp;gt;future&amp;lt;/vp&amp;gt; that represent the remaining &amp;lt;vp&amp;gt;echoLoop_async&amp;lt;/vp&amp;gt; call is simply the &amp;lt;vp&amp;gt;future&amp;lt;/vp&amp;gt; that this call returns:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    echoLoop_async(WebSocket) =&lt;br /&gt;
        WebSocket:readMessage_async():thenDo(&lt;br /&gt;
            { (Message) = F1 :-&lt;br /&gt;
                if webSocket::close(_, _) = Message then&lt;br /&gt;
                    F1 = future::newUnit()&lt;br /&gt;
                else&lt;br /&gt;
                    F1 =&lt;br /&gt;
                        WebSocket:writeMessage_async(Message):thenDo(&lt;br /&gt;
                            { = F2 :-&lt;br /&gt;
                                F2 = echoLoop_async(WebSocket)&lt;br /&gt;
                            })&lt;br /&gt;
                end if&lt;br /&gt;
            }).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can see each &amp;lt;vp&amp;gt;await&amp;lt;/vp&amp;gt; creates a nesting &amp;lt;vp&amp;gt;thenDo&amp;lt;/vp&amp;gt;.  So things that are &amp;lt;vp&amp;gt;N&amp;lt;/vp&amp;gt; await&amp;#039;s into to the future are &amp;lt;vp&amp;gt;thenDo&amp;lt;/vp&amp;gt;-ed &amp;lt;vp&amp;gt;N&amp;lt;/vp&amp;gt; levels deeper.&lt;br /&gt;
&lt;br /&gt;
Future variables like &amp;lt;vp&amp;gt;F1&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;F2&amp;lt;/vp&amp;gt; are only used in one nesting level and therefore they can have the same name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    echoLoop_async(WebSocket) =&lt;br /&gt;
        WebSocket:readMessage_async():thenDo(&lt;br /&gt;
            { (Message) = F :-&lt;br /&gt;
                if webSocket::close(_, _) = Message then&lt;br /&gt;
                    F = future::newUnit()&lt;br /&gt;
                else&lt;br /&gt;
                    F =&lt;br /&gt;
                        WebSocket:writeMessage_async(Message):thenDo(&lt;br /&gt;
                            { = F :-&lt;br /&gt;
                                F = echoLoop_async(WebSocket)&lt;br /&gt;
                            })&lt;br /&gt;
                end if&lt;br /&gt;
            }).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Furthermore, they can often be eliminated by shifting to expressions instead of statements:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    echoLoop_async(WebSocket) =&lt;br /&gt;
        WebSocket:readMessage_async():thenDo(&lt;br /&gt;
            { (Message) = &lt;br /&gt;
                if webSocket::close(_, _) = Message then&lt;br /&gt;
                    future::newUnit()&lt;br /&gt;
                else&lt;br /&gt;
                    WebSocket:writeMessage_async(Message):thenDo(&lt;br /&gt;
                        { = &lt;br /&gt;
                            echoLoop_async(WebSocket)&lt;br /&gt;
                        })&lt;br /&gt;
                end if&lt;br /&gt;
            }).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More about futures (and promises)&lt;br /&gt;
&lt;br /&gt;
* http://docs.scala-lang.org/overviews/core/futures.html&lt;br /&gt;
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise&lt;br /&gt;
&lt;br /&gt;
=== Chaining ===&lt;br /&gt;
&lt;br /&gt;
Notice that in several JavaScript texts often use &amp;#039;&amp;#039;chaining&amp;#039;&amp;#039; instead of deeper nesting (e.g. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises).&lt;br /&gt;
&lt;br /&gt;
Chaining code looks like this (very often formatted like here):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    chaining_async() =&lt;br /&gt;
        something1_async()&lt;br /&gt;
        :thenDo({ (R1) = something2_async(R1) })&lt;br /&gt;
        :thenDo({ (R2) = something3_async(R2) })&lt;br /&gt;
        :thenDo({ (R3) = something4_async(R3) }).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It looks nice because you simply have the operations after each other (embedded in some thenDo-noise, but any way).&lt;br /&gt;
&lt;br /&gt;
Such code does work and it looks relatively nice, so why all the nesting?&lt;br /&gt;
&lt;br /&gt;
Well if you compare to a similar nesting structure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    nesting_async() =&lt;br /&gt;
        something1_async():thenDo(&lt;br /&gt;
            { (R1) =&lt;br /&gt;
                something2_async(R1):thenDo(&lt;br /&gt;
                    { (R2) =&lt;br /&gt;
                        something3_async(R1, R2):thenDo(&lt;br /&gt;
                            { (R3) =&lt;br /&gt;
                                something4_async(R1, R2, R3)&lt;br /&gt;
                            })&lt;br /&gt;
                    })&lt;br /&gt;
            }).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice the important difference that the nested version have access to earlier results.  &lt;br /&gt;
&lt;br /&gt;
In nesting code it is also quite simple to deal with branching code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    nesting_async() =&lt;br /&gt;
        something1_async():thenDo(&lt;br /&gt;
            { (R1) =&lt;br /&gt;
                if test(R1) then&lt;br /&gt;
                    somethingTrue_async(R1)&lt;br /&gt;
                else&lt;br /&gt;
                    somethingFalse1_async(R1):thenDo(&lt;br /&gt;
                        { (R2) =&lt;br /&gt;
                            somethingFalse2_async(R1, R2)&lt;br /&gt;
                        })&lt;br /&gt;
                end if&lt;br /&gt;
            }).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Chaining will only handle &amp;quot;sequential&amp;quot; actions.&lt;br /&gt;
&lt;br /&gt;
So &amp;#039;&amp;#039;chaining&amp;#039;&amp;#039; is actually rather limited, and you might as well get used the nesting method.&lt;br /&gt;
&lt;br /&gt;
(The asynchronous programming constructions in C# and JavaScript corresponds to nesting code, not chaining code).&lt;/div&gt;</summary>
		<author><name>Thomas Linder Puls</name></author>
	</entry>
</feed>