Skip to content

[computed] Support async iterators #14

@julien-f

Description

@julien-f
const Clock = provideState({
  computed: {
    // it simply returns an async iterator instead of a value
    //
    // the computed is undefined before the first
    // value is emitted by the iterator
    //
    // each new emitted value will trigger a new render with the
    // computed set to this value
    //
    // rejections are not handled and will possibly trigger an
    // unhandledRejection event on the window object, the computed will
    // keep its previous value
    //
    // this computed, which does not have dependencies, yields a new
    // date value every second
    async *date() {
      while (true) {
        yield new Date()
        await new Promise(resolve => setTimeout(resolve, 1e3))
      }
    }
  }
})(injectState(({ state }) => <p>{state.date.toISOString()}</p>))

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