type OptimisticRollbackOptions = { save: () => Promise; expected: T; getCurrent: () => T; rollback: () => void; }; export async function saveWithOptimisticRollback({ save, expected, getCurrent, rollback, }: OptimisticRollbackOptions): Promise { try { await save(); } catch (error) { if (Object.is(getCurrent(), expected)) { rollback(); } throw error; } } export function scheduleSaveWithOptimisticRollback( options: OptimisticRollbackOptions, ): void { void saveWithOptimisticRollback(options).catch(() => undefined); }