Closed tenbits closed 6 years ago
Thanks for your interest! It sounds like this is a separate proposal, not a bug report or discussion topic on this proposal, so let me close this thread.
@domenic I'm wondering, why to provide a new syntax, when it would be better to extend the lexical grammar with a keyword, let's say, parallel
.
So instead
const result = await worker({|
const res = await fetch("people.json");
const json = await res.json();
return json[2].firstName;
|});
we have old good IIFE
const result = (parallel function () {
const res = await fetch("people.json");
const json = await res.json();
return json[2].firstName;
})();
With arguments it looks imho also better. Instead:
const resultComputer = workerFunction{|
const [endpoint] = arguments;
const res = await fetch(endpoint);
const json = await res.json();
return json[2].firstName;
|};
const result = await resultComputer(endpoint);
we have
const resultComputer = parallel function (endpoint) {
const res = await fetch(endpoint);
const json = await res.json();
return json[2].firstName;
};
const result = await resultComputer(endpoint);
So, do you see any reasons and advantages to extend the syntax? Or would you create a new proposal with parallel
functions. As you would definitely get more attention to the proposal from the community.
Thanks.
Please take a look at #2 for that question :)
Some thoughts about this topic. Just blocks are not enough. Normally, you orginize your code in reusable functions.
Often the worker function is passed by ref
Now our
Do
logic is thread independent - better to maintain and to cover with unit tests.But in JavaScript there are no locks, semaphores and mutexes, so the idea with scope-clojure-less blocks is great, but I would extend it to functions or lambdas with a
parallel
keyword, just the same asasync
.Arguments
The method would receive arguments as usual function.
ConcurrentDictionary
,ConcurrentQueue
,ConcurrentStack
etc.this
Apparently
no-this
for parallel functions or lambdas.Lexical scoping
Same as this proposal: only arguments.
Imports
import
statements should be allowed on top level of the function. The modules are evaluated in the cluster scope in which theparallel
function is being executed.Execution
There can be 3 options. Which one is better, is the subject to consider:
Parallel functions can be executed with some
Cluster API
. Aparallel
keyword would only ensure asafe
environment (see above) for the method.Parallel functions are always executed by the engine in new cluster.
Mixed: Force parallel functions to be automatically executed in a new cluster, but only if
Cluster.isMaster === true
, (UI Cluster).I would actually prefer this option
parallel
functions never block UI Cluster.async
functionCluster API
for forked clusters, so that Developer can deside in place, if a new cluster is required or not.