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

Adopting state without .then() #270

Open
paulstelian97 opened this issue Jul 8, 2018 · 2 comments
Open

Adopting state without .then() #270

paulstelian97 opened this issue Jul 8, 2018 · 2 comments

Comments

@paulstelian97
Copy link

paulstelian97 commented Jul 8, 2018

I have made my own promises library which attempts to expose an API similar to Javascript's own promises. However there is one thing which your spec doesn't provide (or I am confused): if the original promise (the one made with e.g. the constructor) will resolve with a thenable, what should the first .then() callback give?

const promise1 = new MyPromise(resolve => resolve(5));
const promise2 = new MyPromise(resolve => resolve(promise1));
const final = new MyPromise(resolve => resolve(promise2)).then(val => console.log(val))
const final2 = new MyPromise(resolve => resolve(promise2)).then().then(val => console.log(val))

What should be displayed for the first final promise? Should it be 5 or one of promise1 or promise2? The second one will display 5 anyway.

@bergus
Copy link

bergus commented Jul 9, 2018

The Promises/A+ then method does not support fulfilling with thenables (when returning them from the then callback`). This spec does not say anything about the constructor, it doesn't answer your question.

You can do either. You should decide whether the callback in your constructor is a resolve or a fulfill. You got three choices:

  • It fulfills the promise with the passed value. In that case final should be logging the promise2.
  • It resolves the promise, using thenable assimilation. In that case final should be logging 5.
  • It resolves the promise by adopting the passed promise. In that case final should reject, because promise1 and promise2 are rejected promises, because 5 was not a valid promise.

@paulstelian97
Copy link
Author

I'm pretty sure promise1 should resolve to the value 5, but otherwise thank you. I have chosen to call then() in the constructor (without that final would resolve to promise2 in my implementation)

Note that it passes all tests in the tests repo either way (it's inspired by RWPromises)

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

2 participants