Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Worker methods to return promises #3

Closed
darsain opened this issue Jul 19, 2013 · 11 comments
Closed

Worker methods to return promises #3

darsain opened this issue Jul 19, 2013 · 11 comments
Assignees
Milestone

Comments

@darsain
Copy link

darsain commented Jul 19, 2013

Best would be probably to feature detect the native promises spec, and if present, return them from methods. People can than choose themselves to include polyfills.

A lot better than inlining it into the code, or adding a dependency...

@rudylattae
Copy link

+1

@evanworley
Copy link

+1

(BTW, love this project. Just suffered through doing native web workers), with an ugly fallback for IE9.

@padolsey
Copy link
Owner

I like the idea of only using promises if the native API is available (having to include a promise lib as a dependency was why I avoided it thus far). +1 from me, I'll take a look.

@evanworley
Copy link

Yeah I hear you, and I appreciate that there are no external dependencies. It might be tricky to support folks that don't want promises, and people who want a jquery deferred.

@ghost ghost assigned padolsey Jul 19, 2013
@darsain
Copy link
Author

darsain commented Jul 19, 2013

@evanworley jQuery.Deferred is not a valid Promises spec implementation :) Or at least it wasn't last time I checked (few months ago).

@evanworley
Copy link

I'm fine with detecting and using native/polyfilled promises, I can work with that.

@Raynos
Copy link
Contributor

Raynos commented Jul 20, 2013

If you support promises please support both promises & callbacks.

@padolsey
Copy link
Owner

Ok, here's what I am proposing to solve the whole callbacks/promises issue:

There will be three ways to "fulfil" (successfully return) something from within the worker.

Direct

var o = operative({
  foo: function() {
    return 123;
  }
});
o.foo(function(err, result) {
  result; // => 123
});

Async Basic

var o = operative({
  foo: function() {
    var finish = this.async():
    // do async stuff ...
    finish(456);
  }
});
o.foo(function(err, result) {
  result; // => 456
});

Async Promise

var o = operative({
  foo: function() {
    var resolver = this.deferred():
    // do async stuff ...
    resolver.fulfil(456);
  }
});
o.foo().then(function(result) {
  result; // => 456
});

There will be two different ways to reject from within a worker:

Via Exception

var o = operative({
  foo: function() {
    // ...
    throw new Error('something');
  }
});
o.foo(function(err, result) {
  err; // => 'something'
});

Via reject() (promise)

var o = operative({
  foo: function() {
    var resolver = this.deferred();
    // ...
    resolver.reject('something');
  }
});
o.foo().then(function() {
  // fulfil callback (not called in this case)
}, function(err) {
  err; // => 'something'
});

We will detect if you use async() or deferred() from within an operative method and ensure that we don't expect a direct return in that case.

Assuming this can all be implemented (I'm already in the process of doing this in a branch) is everyone happy with this API?

I guess this'll have to be a 1.0.0 release because technically these are backwards-incompatible changes (specifically the cb(err, res) (#5) change).

@padolsey
Copy link
Owner

After posting that I realised that we'll probably want event a direct return to fulfil a promise too? If so, that's gonna be tricky to reconcile with the existing regular-callback style. To be honest I prefer the explicit this.deferred() style because it means we can be sure that we're NOT expecting a direct-return and that we're NOT gonna have to worry about calling any callbacks. Let me know your thoughts...

@evanworley
Copy link

@padolsey The pull request looks good. Regarding your concern above for the direct return, I'd advocate for the same behavior whether or not any asynchronous code is executing inside the web worker. This way if the operative code changes in the future, say a synchronous block becomes asynchronous, not much will need to change.

@padolsey
Copy link
Owner

Merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants