-
Notifications
You must be signed in to change notification settings - Fork 266
promises #38
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
promises #38
Conversation
|
Yes, I already facing some problem while using |
| return; | ||
| return Promise.all(promises).then(() => { | ||
| if (callback) { | ||
| return callback(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If something inside the callback function throws then the error might get swallowed.
| onUpdate: Listener = cb => { | ||
| this.setState(DUMMY_STATE, cb); | ||
| onUpdate: Listener = () => { | ||
| return new Promise(resolve => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not just return this.setState(DUMMY_STATE); (instead of creating a new promise)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because then we dont wait for the callback
|
@jamiebuilds I tried this in our app and it works perfectly. |
|
Why not pick one or the other? There is no need for 2 different mechanisms to notify on update, and the promise version is cleaner and easier to test. const callback = () => console.log('state updated')
// with callback
setState({
some: 'newValue',
}, callback)
// with promise
setState({
some: 'newValue',
}).then(callback)
// sooner or later, someone will do this
const promise = setState({
some: 'newValue',
}, callback)
promise.then(callback) |
|
@jamiebuilds Is there any update on this? I'd love to see this PR merged in -- it makes the |
|
@ldthorne This isn't consistent, which is fine, but is different from the behavior of React. React's API does not return a promise, it uses the callback style. React's API:
Unstated since version 2:
This PR:
|
|
@jamiebuilds can you create a new release for this on NPM? We'd love to start using this in production |
|
@jamiebuilds What about this case? |
|
@nfort fixed in 2.1.1 |
This re-implements parts of
setStateso that it returns a promise It simplifies a lot about the implementation. The only problem is that we always pass a callback to React'ssetStatewhich may be a perf issue for some apps.