Skip to content

noSvelteIgnoredUnsubscribe (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noSvelteIgnoredUnsubscribe": "error"
}
}
}
}

Disallow ignoring the function returned by a store’s subscribe() method.

Calling subscribe() on a Svelte store returns a function that stops the subscription. If you don’t keep this function, you can never stop the subscription: the store keeps calling your callback and keeps it in memory after you no longer need it. This is a memory leak.

The rule reports a subscribe() call when its result is not used, for example when it isn’t saved in a variable, returned, or passed to another function.

<script>
import { count } from "./stores.js";
count.subscribe((value) => {
console.log(value);
});
</script>
code-block.svelte:4:7 lint/nursery/noSvelteIgnoredUnsubscribe ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The function returned by subscribe() is ignored.

2 │ import { count } from "./stores.js";
3 │
> 4 │ count.subscribe((value) => {
│ ^^^^^^^^^
5 │ console.log(value);
6 │ });

ℹ Without this function, the subscription can't be stopped, so the store keeps calling the callback and keeps it in memory after it's no longer needed.

ℹ Save the returned function and call it when you no longer need the subscription, for example when the component is destroyed.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

<script>
import { onDestroy } from "svelte";
import { count } from "./stores.js";
const unsubscribe = count.subscribe((value) => {
console.log(value);
});
onDestroy(unsubscribe);
</script>