Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including it-pair with all npm packages installed. Try it out:

var itPair = require("it-pair")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

it-pair v1.0.0

pair a source async iterable stream and a sink async iterable stream

it-pair

A pair of {source, sink} streams that are internally connected, (what goes into the sink comes out the source)

This can be used to construct pipelines that are connected.

var pipe = require('it-pipe')
var pair = require('it-pair')

var p = pair()

//read values into this sink...
pipe([1, 2, 3], p.sink)

//but that should become the source over here.
const values = await pipe(p.source, async source => {
  const values = []
  for await (const value of source) {
    values.push(value)
  }
  return value
})

console.log(values) //[1, 2, 3]

This is particularly useful for creating duplex streams especially around servers. Use pull-pair/duplex to get two duplex streams that are attached to each other.

var DuplexPair = require('pull-pair/duplex')

var d = DuplexPair()

//the "client": pipe to the first duplex and get the response.
pipe(
  [1,2,3],
  d[0],
  source => {
    for await (value of source) {
      console.log(value) // => 10, 20, 30
    }
  }
)

//the "server": pipe from the second stream back to itself
//(in this case) appling a transformation.
pipe(
  d[1],
  source => (async function * () {
    for await (const e of source) {
      yield e*10
    }
  })(),
  d[1]
)

License

MIT

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free