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

support promises resolve/reject #46

Closed
searls opened this issue Nov 2, 2015 · 7 comments
Closed

support promises resolve/reject #46

searls opened this issue Nov 2, 2015 · 7 comments

Comments

@searls
Copy link
Member

searls commented Nov 2, 2015

I got an email asking for promise support. I didn't think hard about it because I don't have an immediate need.

What if we had this API?

td.config(promiseProvider: someFuncThatCreatesPromises)

td.when(...).thenResolve(someValue)
td.when(...).thenReject(someOtherValue)

And then maybe on the verification side?

td.verify.resolved(value)
td.verify.rejected(someOtherValue)

I don't use promises much and I generally don't TDD with them in mind (since I try to keep async stuff to the periphery of my app where I'm integrating with stuff). Any thoughts on how to go about this or what an example test would look like?

Paging @jasonkarns as he's my promise whisperer

@searls
Copy link
Member Author

searls commented Mar 10, 2016

I'm still back and forth on this. Going to keep chewing on this post 1.0.0

@Turbo87
Copy link
Contributor

Turbo87 commented Mar 22, 2016

Essentially that would be a shortcut for td.when(...).thenReturn(Promise.resolve(someValue)).

The problem with this is: what is Promise? On more recent node versions it is built-in, but older ones would fail unless polyfilled and the same is true for browsers. Then there are also libraries like RSVP.js that offer a Promise with additional functionality like a finally() method, which is however not attached to the global object.

To support these cases it would probably make sense to add a second, optional parameter to thenResolve/Reject() for passing in the Promise class. If not specified it would then use the built-in one.

@searls
Copy link
Member Author

searls commented Mar 30, 2016

@Turbo87, yes, if this is implemented then the behavior would be:

  1. If no native Promise is found, raise an error
  2. If a top-level Promise function is found, use that as the constructor
  3. If the user has set td.configure({promiseConstructor: SomePromiseFunc}), then use that, regardless of whether a native one is found.

@rowanu
Copy link

rowanu commented Jun 9, 2016

This interface sounds really useful - setting up doubles for Promises takes a bit of boilerplate.

Is it planned for any time soon? Are you happy with the proposed interface, and accepting PRs?

@searls
Copy link
Member Author

searls commented Jun 9, 2016

@rowanu yeah, I'm open to seeing a PR on this.

@mgryszko
Copy link
Contributor

mgryszko commented Jun 23, 2016

I've started working on this issue. See the progress in my fork. Currently, only native Promise is supported.

@searls
Copy link
Member Author

searls commented Jun 24, 2016

Thanks @mgryszko -- I don't think I can merge unless we make the Promise constructor configurable. Sadly we live in the era of transpilers & polyfills and I don't know anyone in practice who uses the native Promise object in production

searls added a commit that referenced this issue Jul 13, 2016
@searls searls mentioned this issue Jul 13, 2016
5 tasks
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

4 participants