padolsey / operative

:dog2: Seamlessly create Web Workers
MIT License
655 stars 45 forks source link

Support for 'throw' inside worker #29

Open YarnSphere opened 9 years ago

YarnSphere commented 9 years ago

Hi! First of all, I've only been using this library for a few days and I already find it awesome, so: thank you for creating it, your work is definitely appreciated! My issue can probably be considered a request: I'm not certain that this is possible (I believe it should be), since I haven't looked into the source. What I would like to do is the following:

var test = operative(function() {
  throw 'Error'; // Throw an error instead of using deferred.reject
});

test().then(
function() {},
function(e) {
  console.error(e);
});

Is this possible? Is this not a good idea? Thank you for your time and for your work!

padolsey commented 9 years ago

Glad you're enjoying Operative :)

Throwing inside of an operative method should probably already result in a rejected promise but somehow I missed that, so thanks for bringing it up! I'll try to get this into 0.4.0rc2 (and formally 0.4.0) which is currently being worked on.

padolsey commented 9 years ago

Actually, thinking about this more...

I'm not sure of the correct semantics here. Plus I think there needs to be more thought given to what happens in the non-promise case. There is always an implicit promise, but if people aren't listening for a rejection then an error might not be noticed at all... which is definitely bad.

Out of curiosity, what is your use case? Is it the case that your operative-method's logic throws legitimate errors? If so, can you just formally reject() it internally?

YarnSphere commented 9 years ago

I'm not sure about the semantics either. After making the request I also thought a bit more about it and wasn't so sure whether it should be implemented.

In my case, I was trying to write functions that could be used both inside or outside a web worker. Raising exceptions is common practise when writing a regular function - I'd like to be able to also see the raised exception if the function is running inside a worker. Using deferred.reject makes no sense in a non-worker environment.