2 ms·
let's say that for an incoming http request, we must always reply with 'ok', and then do some work like send an email. in most languages def a_unction(req
by ihasprotocol 12y ago
let's say that for an incoming http request, we must always reply with 'ok', and then do some work like send an email.
in most languages
def a_unction(request):
work()
return foo
fuctional languages
(defn a-function
[req]
(work)
foo)
erlang
a_function(Req)->
Req ! foo,
work().
nodejs has callbacks but is single threaded so cannot be compared with erlang.
nodejs
function a_function(Req, callback){
callback(foo);
work();
}
more granularity possible in erlang
a_function(Req)->
%% replies async'ly from the current process
Req ! foo,
work().
a_function(Req)->
%% replies async'ly from another process
spawn(fun()-> Req ! foo end),
work().
You can see how processes along with message passing make it trivial to do async right. Also spawning a process in erlang is several times more light weight than creating an os thread.