Skip to content

Instantly share code, notes, and snippets.

@bh3605
Last active February 28, 2026 06:36
Show Gist options
  • Select an option

  • Save bh3605/247148010f3d238d5ed79407ff119646 to your computer and use it in GitHub Desktop.

Select an option

Save bh3605/247148010f3d238d5ed79407ff119646 to your computer and use it in GitHub Desktop.
Promise-based utility that waits for signal readiness, supporting non-null checks or custom type-guard predicates with proper overload inference.
/**
* Converts an Angular signal getter into a Promise that resolves once a
* nonnull value is emitted. Internally creates an effect and
* automatically destroys it after resolution to prevent memory leaks.
*
* @param source A function that returns the current value of a signal.
* @returns A Promise that resolves with the first value the signal emits that isn't null
*/
export function waitForSignal<T>(source: () => T): Promise<NonNullable<T>>;
/**
* Converts an Angular signal getter into a Promise that resolves once a
* specified condition is met. Internally creates an effect and
* automatically destroys it after resolution to prevent memory leaks.
*
* @param source A function that returns the current value of a signal.
* @param predicate A function that determines when the returned value is considered ready.
* @returns A Promise that resolves with the first value that satisfies the predicate.
*/
export function waitForSignal<T, R extends T>(
source: () => T, predicate: (value: T) => value is R
): Promise<R>;
export function waitForSignal<T, R extends T>(
source: () => T, predicate?: (value: T) => value is R
): Promise<R | NonNullable<T>> {
return new Promise((resolve) => {
let ref: EffectRef;
ref = effect(() => {
const value = source();
if(predicate) {
if(predicate(value)) {
ref.destroy();
resolve(value);
}
} else if(value != null) {
ref.destroy();
resolve(value as NonNullable<T>);
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment