Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async update #596

Open
tajnymag opened this issue Sep 4, 2024 · 1 comment
Open

Async update #596

tajnymag opened this issue Sep 4, 2024 · 1 comment

Comments

@tajnymag
Copy link

tajnymag commented Sep 4, 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.

// 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()
  }
@kaaax0815
Copy link

I agree.
Using something like

await Promise.resolve(fn(this.data))

or simply

await fn(this.data);

would be fairly simple.

If this is not wanted. At least do something like

type NotAsyncFunction<T> = T extends (...args: any[]) => Promise<any> ? never : T;

update<K>(fn: NotAsyncFunction<(data: T) => K>): Promise<void> {
  fn(this.data);
  await this.write()
}

to not allow async functions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants