2024-04-17


queueMicrotask()
I had an issue in Svelte 5 where I tried to use .focus() but it only worked in SSR, not when SSR was disabled.
const patternSelector = document.querySelector<HTMLButtonElement>("#nejime button")!;
patternSelector.focus();
Dominic from the Svelte team explained that SvelteKit tries to restore focus and this conflicted with the focus() I was trying to execute. To fix this I could use queueMicrotask() which makes sure my code runs after SvelteKit is done.
const patternSelector = document.querySelector<HTMLButtonElement>("#nejime button")!;
queueMicrotask(() => {
  patternSelector.focus();
});
Thanks Dominic! 😊
Go back to all posts

2023-11-15


Function call and bind
There are two interesting methods: .call() and .bind()
Using function.call() it’s possible to specify what the this value will be.
There’s also a more modern function.bind() which returns a new function. This one also has a this that can be specified.
You can also do function.bind(function, alreadySetValue) for partial functions with pre specified arguments.
Go back to all posts