S
SolidJS3mo ago
jack

does any sort of primitive similar to Show for non-jsx use exist?

i had Claude generate most of this function for me. wondering if there's a solution/pattern for this problem, as well as if this is a horrible idea and i'm missing some case as to why
export function depends<T, U>(
v: () => T | undefined,
fn: (value: NonNullable<T>) => Promise<U>
): () => Promise<U | undefined> {
return async () => {
const value = v();
if (value !== undefined && value !== null) {
return fn(value as NonNullable<T>);
}
return undefined;
};
}
export function depends<T, U>(
v: () => T | undefined,
fn: (value: NonNullable<T>) => Promise<U>
): () => Promise<U | undefined> {
return async () => {
const value = v();
if (value !== undefined && value !== null) {
return fn(value as NonNullable<T>);
}
return undefined;
};
}
so that i can write signals with async inputs a bit easier:
const selections = createAsync(
depends(
() => user()?.id,
async (userId) => getSelections(userId)
)
);
const selections = createAsync(
depends(
() => user()?.id,
async (userId) => getSelections(userId)
)
);
previously i'd have to either do some sort of type guard/narrow manually each time in the function passed here which isn't great, or I just do like user()?id ?? "", but i don't really want to be passing empty string down as a fallback, i'd rather do nothing until user()?.id is valid
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server