hyper-async
v1.2.0
Published
Async is a tiny FP library for javascript. It provides a small, composable abstraction over async effects with predictable error handling and strict composition semantics.
Readme
hyper-async
Async is a tiny FP library for javascript. It provides a small, composable abstraction over async effects with predictable error handling and strict composition semantics.
install
npm install hyper-asyncUsage
Composition and errors
map(fn):fnmust be a function. Sync throws insidefnare caught and become a rejected Async.chain(fn):fnmust return anAsync. If it returns anything else, the chain rejects with aTypeError.bimap(rejFn, resFn): both args must be functions.bichain(rejFn, resFn): both args must returnAsync.fold(rejFn, resFn): both args must returnAsync.fromPromise(f):fmust be a function; sync throws reject theAsync.
Errors never escape; they surface as a rejected Async (and thus a rejected .toPromise()).
import Async from "hyper-async";
const prop = (k) => (o) => o[k];
async function main() {
const x = await Async.of(1)
.chain((n) => Async.fromPromise(fetch)(`https://example.test/posts/${n}`))
.chain((res) => Async.fromPromise(res.json.bind(res))())
.map(prop("title"))
.toPromise();
console.log(x);
}
main();