Asynchronous Programming and Eventhandling in D

O/N/S via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 7 00:49:40 PDT 2016


On Tuesday, 5 July 2016 at 20:38:53 UTC, Eugene Wissner wrote:
> On Tuesday, 5 July 2016 at 08:24:43 UTC, O/N/S wrote:
>> Hi ("GrĂ¼ss Gott")
>>
>> I like the asynchronous events in Javascript.
>> Is something similar possible in D?
>>
>> Found Dragos Carp's asynchronous library 
>> (https://github.com/dcarp/asynchronous).
>> Are there any more integrated (in Phobos/in D) ways to work 
>> asynchronously?
>>
>> An example: One server ask a second server to calculate 
>> something big.
>> The first server continues with his work, until the answer 
>> come back from the second.
>> And so on...
>>
>> Using threads or fibers would be a way, but has not the same 
>> elegancy like the Javascript way. (To avoid discussions: D is 
>> better ;-)
>>
>>
>> Greetings from Munich,
>> Ozan
>
> Can you describe what would you like to see more concretly. I 
> know js but how is it supposed to work for D? Maybe you can 
> give some example, kind of pseudo code? It would help me much 
> to build a concept and maybe we will see someday something 
> usable in this area :)

Hi,
I took a typical example (using jquery lib, other js-libs have 
similar calls)
$.ajax({
     type: "POST",
     url: myurl,
     dataType: "JSON",
     data: $myData,
     async: true,
     success: function(success) { ... },
     error: function(error) { ... }
});

If response is successful -> call function(success)
If response has errors -> call function(error)

In the meanwhile the code can continue without waiting for the 
response.
In D-style it would look like

...do something..
funcAjax(["type":"POST"...], delegateSuccess, delegateError);
- or -
funcAjax(["type":"POST"...], (success) => {...}, (error) => 
{...});
...continue without waiting...

Why delegates not functions? Because responses could require 
changes in data, which are outside (yeah, I know, it's also 
technically possible with function, but that's not the idea 
behind)

Greetings from Munich, Ozan





More information about the Digitalmars-d-learn mailing list