Skip to content

Support for asynchronous values (like Promises and Observables) #6481

Closed
@ghost

Description

At the moment, it is quite cumbersome to work with promises in React. Either you write a lot of code to update the state inside the then() callbacks or you use a library like react-promise which only works for children (not properties) because it is based on components.

Another option is to traverse the whole virtual dom and replace promises before they are passed to React. (I have read once a blog post about this but cannot find it any longer.) This is obviously not ideal for the rendering performance.

Since promises are now a standard, I think there should be simpler way to use them directly in the view. One option would be to introduce an AsyncValue type with a Promise implementation. The AsyncValue type would then be supported for children as well as properties.

Promises could then simply be used like this:

<div>{promise(this.state.myPromise)}</div>

Opposed to supporting promises everywhere (directly without the promise function), this should not have unexpected side effects. Moreover, other types of asynchronous values (for example Observables) could be added later easily just by creating another AsyncType implementation.

Angular 2 takes a similar route with the async pipe. There you can simply write

{{ myPromise | async }}

in attributes as well as normal content.

This feature would also be very convinient for libraries like Flux because it allows to fetch properties directly inside the view and avoids the reptition which you have with Relay:

<div>{promise(this.state.model.get('property'))}</div>

What do you think about this proposal?
Has this been discussed before? I couldn't find a thread which covers this particular topic.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions