diff --git a/src/derive.ts b/src/derive.ts index 793a52f..96727d0 100644 --- a/src/derive.ts +++ b/src/derive.ts @@ -18,6 +18,11 @@ type AwaitAtomsValues< [Index in keyof TTuple]: Awaited>; }; +/** + * Awaits all `deps` if necessary, then runs `op` given all deps in the same order. + * If computing the value fails (throws), a rejected Promise is returned no matter if + * the processing happened synchronously or not. + */ export function derive< TDeps extends readonly [Atom, ...Atom[]], TValue, diff --git a/src/soonAll.ts b/src/soonAll.ts index 2d149fb..44fe64b 100644 --- a/src/soonAll.ts +++ b/src/soonAll.ts @@ -14,14 +14,19 @@ function isKnown(value: ExtraPromise | S): boolean { return true; // not a promise, we know the value. } +/** + * Given array `values`, if all elements are known (are not unresolved promises), + * returns an array of the same length with Awaited `values`. Otherwise, it returns a + * promise to that array. + */ export function soonAll( - input: T, + values: T, ): SoonAll { - if (input.every(isKnown)) { - return input.map((el) => + if (values.every(isKnown)) { + return values.map((el) => isPromise(el) ? el.value : el, ) as unknown as SoonAll; } - return Promise.all(input); + return Promise.all(values); }