async functions / examples
Is it possible to use parallel.js with function that have take a callback to deliver the results?
No(t yet). Is the call to the callback synchronous or async? If it's synchronous you could work around it with something like that:
var x = null;
func(function(data) {x = data});
return x;
I'm not sure how we can integrate something like that in a backward-compatible way. We might have to provide a callback function to all given parallel.js functions to achive something like that.
The callback might be async. (It is definitely in some of my implementations I want to use parallel.js for).
I think Mocha does something to detect the number of arguments the given function accepts and then decides whether it is a sync or async test.
That might be a nice and backwards compatible solution for parallel.js.
Seems like detecting whether or not the function has 2 parameters and we should wait for the callback to be invoked is rather easy: http://stackoverflow.com/a/4833465/856403
Ah, that might be possible then. At first I was thinking about parsing the function head, but the length property should make this pretty easy.
I'm not sure when I'll get to it, so I'll mark it up for grabs for now.
Maybe I'm missing something but why not just use .then?
Sent from my iPhone
On May 3, 2013, at 10:57 AM, Sebastian Mayr [email protected] wrote:
Ah, that might be possible then. At first I was thinking about parsing the function head, but the length property should make this pretty easy.
I'm not sure when I'll get to it, so I'll mark it up for grabs for now.
— Reply to this email directly or view it on GitHubhttps://github.com/adambom/parallel.js/issues/39#issuecomment-17408794 .
@adambom We meant a async function in the worker itself.
I'm actually not really sure which async functions you could access in a worker context, but I can imagine XmlHttpRequest being accessible, or a library function you're importing which you don't want to change the API of.
ajax is the key one, but being able to call a callback is also helpful if the worker wants to use a transferable arrays in the worker's response, they way I dealt with this in communist is to create a callback function which accepts two arguments, the data and anything to transfer, and is passed as the second argument, the function is then run and if it's return value is defined the callback is called on the result. This also helps deal with the browsers that throw a fit if you try to use transferable objects (IE 10) by letting you add another level of abstraction. The main downside is that functions can't return undefined as the worker will just wait for the callback (functions can call the callback with no arguments though.
The code I used is bellow, feel free to use it though I haven't looked through the parallel code in a while so I have no idea how well it fits in.
_self.cb=function(data,transfer){
!self._noTransferable?self.postMessage(data,transfer):self.postMessage(data);
};
self.onmessage=function(e){
_self.result = _db.__fun__(e.data,_self.cb);
if(typeof _self.result !== "undefined"){
_self.cb(_self.result);
}
}
+1 This library would become extremely powerful if it had the ability to do async functions inside a Parallel function. Obviously the Parallel function would need to utilize a callback.
Edit: I'm looking at it more from a node.js standpoint of doing massive computations on big data that would first need to be fetched by a worker.
+1 on this as well - looking at it from the same perspective as @nickpoorman.
Is there still no support for it?