Skip to content
Lucent
SEARCH LUCENT

Search guides, APIs, and examples.

GitHub

Very early and experimental. The language, the generated native code and every package API change without notice. Do not use Lucent in production.

Cancel work

Take an AbortSignal from JavaScript and check it; the promise rejects with an AbortError.

search.lucent.ts
import { delay } from "lucent:core";
export async function search(
words: string[],
query: string,
signal: AbortSignal,
): Promise<string[]> {
const found: string[] = [];
for (let i = 0; i < words.length; i++) {
if (i % 1000 === 0) {
signal.throwIfAborted();
await delay(0); // let other calls in between chunks
}
const word = words[i]!;
if (word.includes(query)) found.push(word);
}
return found;
}

Pass an AbortSignal from JavaScript. When JavaScript calls abort(), the signal is set in native code at once. Your code stops where it checks: signal.throwIfAborted(), or a delay(ms, signal).

  • The rejection is an Error named AbortError, or the reason JavaScript passed to abort().
  • signal.aborted reads the state, and signal.addEventListener("abort", …) runs a function when it's set.
  • A signal can come into Lucent, but not go back out, and an AbortController can't cross at all.