fromPromise which starts with fulfilled state
See original GitHub issueWe mostly use fromPromise
to render loading or other states in react components. Here is a sample component:
@observer
class MyComponent extends React.Component {
@action componentDidMount() {
this.entity = this.props.store.getEntity(); // getEntity returns a `IPromiseBasedObservable`
}
@observable entity = fromPromise(Promise.resolve());
render() {
return this.entity.case({
fulfilled: value => (<div>{JSON.stringify(value)}</div>),
rejected: error => (<div>Error</div>),
pending: () => (<div>Loading entity...</div>)
});
}
}
I have initialized entity
with a resolved fromPromise
to avoid writing an if
in render
method. What I think is really useful is fromPromise(value)
. So fromPromise
does not necessarily needs a Thenable
object.
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
fromPromise · MobX Guide - mamaya-spark
FULFILLED . Observable promises can be created immediately in a certain state using fromPromise.reject(reason) or fromPromise.resolve(value?)
Read more >Promise.race() - JavaScript - MDN Web Docs
A Promise that asynchronously settles with the eventual state of the first promise in the iterable to settle. In other words, it fulfills...
Read more >JS Promises: Fulfill vs Resolve - Stack Overflow
An unresolved promise is always in the pending state. A resolved promise may be pending, fulfilled or rejected.
Read more >JavaScript | Promise.allSettled() Method - GeeksforGeeks
The Promise is a JavaScript object which can be in three states pending, fulfilled or rejected. The Promise.allSettled() method in ...
Read more >How to use the mobx-utils.fromPromise.resolve function in mobx ...
To help you get started, we've selected a few mobx-utils. ... state = { observableFetchPromise: fromPromise.resolve(), }; render() { return this.state.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Just to add, in some circumstances it can be handy to create a PromiseBasedObservable in a static pending or rejected state too. For example:
3.0.0 introduced the methods
fromPromise.reject(value)
andfromPromise.resolve(value)
.