packages/docs/src/routes/tutorial/events/synchronous-sync/index.mdx
While not a common use case, you may occasionally need to process events synchronously.
The two most common use cases are to call event.preventDefault() or event.stopPropagation() on a DOM event. Those are handled by adding the preventdefault:eventname or stoppropagation:eventname in addition to your event handler.
<a href="/" preventdefault:click onClick$={() => {
console.log('clicked');
}}>
link
</a>
When you have other use cases that require synchronous event processing, you can use either sync$() or useVisibleTask$().
sync$() to embed the code in the HTML. Fast, resumable, but with some restrictions (see below).useVisibleTask$() to register a handler. No restrictions, but it will not handle events until the visible task has executed, and it will cause of course run extra javaScript while the page is loading. <a href="/"
onClick$={sync$((event, target) => {
// Only prevent default if the ctrl key is pressed.
if (event.ctrlKey) {
event.preventDefault();
}
})}>
link
</a>
sync$() RestrictionsThe sync$() function is a resumable way to process events synchronously. However, it has some significant restrictions. The sync$() can't close over anything. The implications are that you can't:
For this reason, we recommended breaking up the event handlers into two parts:
<a href="/"
onClick$={[
sync$((event, target) => {
// This part is synchronous and can't close over anything.
if (event.ctrlKey) {
event.preventDefault();
}
}),
$(() => {
// This part can be asynchronous and can close over anything.
console.log('clicked');
})
]}>
link
</a>
You can also dispatch a custom event from the sync$() handler and handle it asynchronously:
<a href="/"
onClick$={sync$((event, target) => {
if (event.ctrlKey) {
event.preventDefault();
}
// Dispatch a custom event to be handled asynchronously.
target.dispatchEvent(new CustomEvent('myclick', { bubbles: true }));
})}
onMyclick$={() => {
// This part can be asynchronous and can close over anything.
console.log('clicked');
}}>
link
</a>
Your task: Convert the onClick$ from asynchronous event to synchronous event by using sync$.