#596·lowdb

Async update

Author: tajnymagCreated Sep 4, 2024Updated Sep 18, 2024

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.

typescript
// 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()
  }