Scramjet v4.22 brings requested all
and race
methods, v4.23 fixes the TypeScript API for some optional parameters.
New methods are:
DataStream..all
- processes a number of functions in parallel, returns a stream of arrays of results.DataStream..race
- processes a number of functions in parallel, returns the first resolved.The new methods follow a similar logic to Promise.all and Promise.race.
A simple example of this is calling a couple URLs in parallel for each entry:
_10const fetch = require("node-fetch");_10const { DataStream } = require("scramjet");_10const { reportLocally } = require("./lib");_10_10DataStream.from(data)_10 .all([entry => fetch(entry.reportURL), entry => fetch(entry.dataURL), reportLocally])_10 .consume();
A similar effect can be achieved with promises:
_10DataStream.from(data)_10 .map(entry => Promise.all([fetch(entry.reportURL), fetch(entry.dataURL), reportLocally(entry)]))_10 .consume();
race
method works similarly to Promise.race
so simply continues whenever the first of all transforms resolves. This could be useful when we just want to confirm one of some things happened, like this:
_10DataStream.from(data)_10 // we don't care which report is done first as long as at least one is._10 .race([reportLocally, reportRemotely])_10 .consume();