fx

Utilities to handle complex async flow control


fx (pronounced Effects) are helper functions to make async flow control easier.

parallel #

The goal of parallel is to make it easier to cooridnate multiple async operations in parallel, with different ways to receive completed tasks.

All tasks are called with fx.safe which means they will never throw an exception. Instead all tasks will return a Result object that the end development must evaluate in order to grab the value.

1import { parallel } from "starfx";
2
3function* run() {
4  const task = yield* parallel([job1, job2]);
5  // wait for all tasks to complete before moving to next yield point
6  const results = yield* task;
7  // job1 = results[0];
8  // job2 = results[1];
9}

Instead of waiting for all tasks to complete, we can instead loop over tasks as they arrive:

1function* run() {
2  const task = yield* parallel([job1, job2]);
3  for (const job of yield* each(task.immediate)) {
4    // job2 completes first then it will be first in list
5    console.log(job);
6    yield* each.next();
7  }
8}

Or we can instead loop over tasks in order of the array provided to parallel:

1function* run() {
2  const task = yield* parallel([job1, job2]);
3  for (const job of yield* each(task.sequence)) {
4    // job1 then job2 will be returned regardless of when the jobs
5    // complete
6    console.log(job);
7    yield* each.next();
8  }
9}
<< PREV
Loaders
NEXT >>
Error Handling