question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fromPromise which starts with fulfilled state

See original GitHub issue

We 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:closed
  • Created 7 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jamiewindercommented, Mar 15, 2017

Just to add, in some circumstances it can be handy to create a PromiseBasedObservable in a static pending or rejected state too. For example:

@computed get data(): IPromiseBasedObservable<Data> {
    const serverData = this.serverData;
    if (serverData.state != REJECTED) {
        return fromPromise.state(serverData.state); // *
    }
    // process local data + server data
}

@computed get serverData(): IPromiseBasedObservable<Data>{
    return fromPromise(fetchServerData);
}
0reactions
mweststratecommented, Jul 25, 2017

3.0.0 introduced the methods fromPromise.reject(value) and fromPromise.resolve(value).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found