promise-or-value
This module is designed around the idea of a custom TypeScript type:
;
The motivations for this type is because Promise
s resolve asynchronously, even if you call then
on an already-
resolved promise (e.g. Promise.resolve(5).then(() => …)
).
Promises work this way by design! It avoids bugs.
But for performance-critical code where you're working with cacheable async data in a loop, it can be too slow.
API
then(pov, onValue, onError)
- "fast" equivalent of Promise.resolve(pov).then(onValue).catch(onError)
;
Run some logic immediately on values, or later, for promises.
all(povs)
- "fast" equivalent of Promise.all(povs)
;
Returns array as-is if everything is a value, or calls Promise.all
if there are any promises in the array
PromiseOrValueMapLike
- for caching promise data
;
A type used by getOrAdd
. A simple conforming cache can be made with new Map<K, PromiseOrValue<V>>()
.
getOrAdd
- for working with PromiseOrValueMapLike
;
If the key exists in the cache, returns it, otherwise computes it and inserts it in the cache. If the computation returns a promise, the promise is inserted in the cache, and replaced with the literal value once it resolves.