Skip to content

until

ts
until<T>(service: Service<T>, predicate: (props: T) => boolean): Promise<T>

A one-shot wait on a service.

Usage

ts
import { 
until
,
useScroll
} from '@studiometa/js-toolkit';
async function
afterScroll
() {
const
props
= await
until
(
useScroll
(), ({
isScrolling
}) => !
isScrolling
);
return
props
.
y
;
}

Parameters

  • service — a Service<T>, and nothing else.
  • predicate — called with each update.

Return value

  • a promise resolving with a copy of the props on the first update that matches.

The contract

  • It releases the subscription before it resolves, so an awaiting caller never holds a source open.
  • It resolves with a copy, because the props object belongs to its service and is only valid for the duration of the call that received it.
  • It resolves on the current props when they already match, through the service's hasProps() — so await until(useScroll(), ({ isScrolling }) => !isScrolling) on a still page returns immediately rather than waiting for a scroll that never comes.

When to reach for it

When the code reads as a step in a sequence rather than a reaction:

js
await until(useScroll(), ({ isScrolling }) => !isScrolling);
this.measure();

For a reaction, subscribe. For an ongoing condition, subscribe and act on each delivery.

MIT Licensed