Skip to content

Async update #596

@tajnymag

Description

@tajnymag

Currently .update() awaits only the this.write() operation. It would be nice for it to also await the passed callback which mutates the state. Without the await, the persisted value could occur before the callback finishes its mutation.

// before
async update(fn: (data: T) => unknown): Promise<void> {
    fn(this.data)
    await this.write()
}

// after
async update(fn: (data: T) => unknown): Promise<void> {
    const result = fn(this.data);
    if (result instanceof Promise) {
      await result;
    }
    await this.write()
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions