How to use the take function from pull-stream

Find comprehensive JavaScript pull-stream.take code examples handpicked from public code repositorys.

pull-stream.take creates a stream that reads a specified number of items from an input stream, then ends the stream.

208
209
210
211
212
213
214
215
216
   take(3)
--1---2--3|
```
```js
const source = pull.count()
const through = pull.take(3)
const sink = pull.log()
pull(source, through, sink)
```
fork icon46
star icon839
watch icon48

114
115
116
117
118
119
120
121
122
123
if (!msgCount) return cb(null, -1)

let result = -1
pull(
  oldLog.getStream({ gte: 0 }),
  pull.take(msgCount),
  pull.drain(
    (x) => {
      result = x.seq
    },
fork icon6
star icon41
watch icon0

How does pull-stream.take work?

pull-stream.take is a higher-order stream that returns a new stream that only passes through a specified number of values from the source stream before closing. It takes a single argument that specifies the number of values to take. Once the specified number of values are taken, the new stream will close, ending the flow of data from the source stream.

564
565
566
567
568
569
570
571
572
573
sbot.db2migrate.progress(),
pull.filter((progress) => {
  console.log(progress)
  return progress === 1
}),
pull.take(1),
pull.drain(async () => {
  endMeasure(t, 'migrate (+db1)')
  await sleep(2000) // wait for new log FS writes to finalize
  sbot.close(true, () => ended.resolve())
fork icon6
star icon41
watch icon0

+ 7 other calls in file

107
108
109
110
111
112
113
114
115
116
          .catch(err => cb(err))
      }, 5),
      pull.filter(Boolean),
      // TODO: This removes the profiles that came back as null, we might want to show something in place of that
      // e.g. someone who hasnt opted in to publicWebHosting
      limit ? pull.take(limit) : null,
      pull.collect((err, res) => err ? reject(err) : resolve(res))
    )
  })
}
fork icon2
star icon3
watch icon0

+ 11 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const pull = require("pull-stream");
const source = pull.values([1, 2, 3, 4, 5]);
const limit = 3;

pull(
  source,
  pull.take(limit),
  pull.collect((err, data) => {
    if (err) {
      console.error(err);
    } else {
      console.log(data); // prints [1, 2, 3]
    }
  })
);

In this example, pull.values creates a pull stream from an array of numbers, and pull.take limits the number of values that are passed down the stream to three. Finally, pull.collect collects all the values that pass through the stream and logs them to the console. The output of the program is [1, 2, 3].

36
37
38
39
40
41
42
43
44
})

function sendMessages (local, remote) {
  pull(
    pull.infinite(),
    pull.take(100),
    pull.map((val) => Buffer.from(val.toString())),
    local
  )
fork icon0
star icon1
watch icon2

+ 3 other calls in file

2
3
4
5
6
7
8
9
10
11
// const array = require('../smallArray')
const pull = require('pull-stream')

const stream = pull(
  pull.infinite(),
  pull.take(10000),
  pull.collect((err, ary) => {
    console.log('completed')
  })
)
fork icon0
star icon0
watch icon1