parallel.js icon indicating copy to clipboard operation
parallel.js copied to clipboard

async functions / examples

Open tp opened this issue 13 years ago • 10 comments

Is it possible to use parallel.js with function that have take a callback to deliver the results?

tp avatar May 03 '13 14:05 tp

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.

Sebmaster avatar May 03 '13 15:05 Sebmaster

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.

tp avatar May 03 '13 17:05 tp

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

tp avatar May 03 '13 17:05 tp

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.

Sebmaster avatar May 03 '13 17:05 Sebmaster

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 avatar May 03 '13 21:05 adambom

@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.

Sebmaster avatar May 03 '13 21:05 Sebmaster

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);
    }
}

calvinmetcalf avatar Jun 19 '13 12:06 calvinmetcalf

+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.

nickpoorman avatar Feb 03 '15 15:02 nickpoorman

+1 on this as well - looking at it from the same perspective as @nickpoorman.

bportnoy avatar Apr 26 '15 01:04 bportnoy

Is there still no support for it?

juanluDeveloper avatar Mar 03 '22 12:03 juanluDeveloper