Scramjet v4.23

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:

  const fetch = require("node-fetch");
  const {DataStream} = require("scramjet");
  const {reportLocally} = require("./lib");

  DataStream.from(data)
    .all([
      (entry) => fetch(entry.reportURL),
      (entry) => fetch(entry.dataURL),
      reportLocally
    ])
    .consume();

A similar effect can be achieved with promises:

  DataStream.from(data)
    .map(
      (entry) => Promise.all([
        fetch(entry.reportURL),
        fetch(entry.dataURL),
        reportLocally(entry)
      ])
    )
    .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:

  DataStream.from(data)
    // we don't care which report is done first as long as at least one is.
    .race([
      reportLocally,
      reportRemotely
    ])
    .consume();