@effection-contrib/fx
A collection of utility functions to streamline asynchronous workflows. These functions were originally part of the starfx project and have been adapted for use with the Effection framework.
API Reference
interface ParallelRet<T> extends Computation<Result<T>[]>
function parallel<T>(operations: Callable<T>[]): Operation<ParallelRet<T>>
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 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.
Examples
Example 1
import { parallel } from "starfx";
function* run() {
const task = yield* parallel([job1, job2]);
// wait for all tasks to complete before moving to next yield point
const results = yield* task;
// job1 = results[0];
// job2 = results[1];
}
Instead of waiting for all tasks to complete, we can instead loop over tasks as they arrive:
Example 2
function* run() {
const task = yield* parallel([job1, job2]);
for (const job of yield* each(task.immediate)) {
// job2 completes first then it will be first in list
console.log(job);
yield* each.next();
}
}
Or we can instead loop over tasks in order of the array provided to parallel:
Example 3
function* run() {
const task = yield* parallel([job1, job2]);
for (const job of yield* each(task.sequence)) {
// job1 then job2 will be returned regardless of when the jobs
// complete
console.log(job);
yield* each.next();
}
}
Type Parameters
T
Parameters
operations: Callable<T>[]
Return Type
Operation<ParallelRet<T>>