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

Use the promise(fn) notation #3

Open
ForbesLindesay opened this issue Dec 21, 2012 · 3 comments
Open

Use the promise(fn) notation #3

ForbesLindesay opened this issue Dec 21, 2012 · 3 comments

Comments

@ForbesLindesay
Copy link
Member

We could have a promise(fn) method which returns a promise, and calls the function with the resolver for that promise.

Advantages

Disadvantages

  • Increased nesting
  • Extra function keyword
  • deffereds will receive a lot less hate once we have ES6

Summary

I'm very much in favour of this proposal

@domenic
Copy link
Member

domenic commented Dec 21, 2012

Given what I discovered in #5, I think this approach should definitely exist. Whether we do deferreds also is a different question, but I'm coming around to a promise constructor idea using this pattern.

@ForbesLindesay
Copy link
Member Author

I think if we do go with this as our primary promise creation method, we need to strictly enforce that fn is called in the same turn of the event loop. That way it would be possible to do something like:

function Deferred() {
  if (!(this instanceof Deferred)) return new Deferred();
  var resolver;
  var promise = new Promise(function (res) { resolver = res; });
  this.resolver = resolver;
  this.promise = promise;
}

@lsmith
Copy link

lsmith commented Jan 14, 2013

@ForbesLindesay Good point about fn being called in the same turn of the event loop. In a Deferred + Promise implementation, the creation of the pair is synchronous, and the setup code to resolve the promise would (or at least could and usually would) then occur directly afterward. For parity with this expectation, it would be worth it to specify that the executor function, as I've taken to calling it, is executed synchronously.

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

3 participants