Skip to content

Instantly share code, notes, and snippets.

@colelawrence
Created December 21, 2023 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colelawrence/40ae8f20c62c32eb729b22257b563436 to your computer and use it in GitHub Desktop.
Save colelawrence/40ae8f20c62c32eb729b22257b563436 to your computer and use it in GitHub Desktop.
Synchronously read a svelte 3/4 store / Readable
import type { Readable } from "svelte/store";
const UNSET = Symbol();
export function read<T>(store: Readable<T>): T {
let value: T | typeof UNSET = UNSET;
store.subscribe((v) => (value = v))();
if (UNSET === value) {
throw new Error("Readable did not react synchronously");
}
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment