operative icon indicating copy to clipboard operation
operative copied to clipboard

Support for 'throw' inside worker

Open YarnSphere opened this issue 9 years ago • 3 comments

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!

YarnSphere avatar Mar 31 '15 20:03 YarnSphere

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 avatar Apr 08 '15 21:04 padolsey

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?

padolsey avatar Apr 09 '15 11:04 padolsey

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.

YarnSphere avatar Apr 09 '15 11:04 YarnSphere