git ssb

2+

Dominic / pull-stream



Tree: 4c7ae55ea2df8d52977d122b65825b9350020303

Files: 4c7ae55ea2df8d52977d122b65825b9350020303 / test / async-map.js

1857 bytesRaw
1var pull = require('../')
2var tape = require('tape')
3tape('async-map', function (t) {
4
5 pull(
6 pull.count(),
7 pull.take(21),
8 pull.asyncMap(function (data, cb) {
9 return cb(null, data + 1)
10 }),
11 pull.collect(function (err, ary) {
12 console.log(ary)
13 t.equal(ary.length, 21)
14 t.end()
15 })
16 )
17})
18
19tape('abort async map', function (t) {
20 var err = new Error('abort')
21 t.plan(2)
22
23 var read = pull(
24 pull.infinite(),
25 pull.asyncMap(function (data, cb) {
26 setImmediate(function () {
27 cb(null, data)
28 })
29 })
30 )
31
32 read(null, function (end) {
33 if(!end) throw new Error('expected read to end')
34 t.ok(end, "read's callback")
35 })
36
37 read(err, function (end) {
38 if(!end) throw new Error('expected abort to end')
39 t.ok(end, "Abort's callback")
40 t.end()
41 })
42
43})
44
45tape('abort async map (async source)', function (t) {
46 var err = new Error('abort')
47 t.plan(2)
48
49 var read = pull(
50 function(err, cb) {
51 setImmediate(function() {
52 if (err) return cb(err)
53 cb(null, 'x')
54 })
55 },
56 pull.asyncMap(function (data, cb) {
57 setImmediate(function () {
58 cb(null, data)
59 })
60 })
61 )
62
63 read(null, function (end) {
64 if(!end) throw new Error('expected read to end')
65 t.ok(end, "read's callback")
66 })
67
68 read(err, function (end) {
69 if(!end) throw new Error('expected abort to end')
70 t.ok(end, "Abort's callback")
71 t.end()
72 })
73
74})
75tape('asyncMap aborts when map errors', function (t) {
76 t.plan(2)
77 var ERR = new Error('abort')
78 pull(
79 pull.values([1,2,3], function (err) {
80 console.log('on abort')
81 t.equal(err, ERR, 'abort gets error')
82 t.end()
83 }),
84 pull.asyncMap(function (data, cb) {
85 cb(ERR)
86 }),
87 pull.collect(function (err) {
88 t.equal(err, ERR, 'collect gets error')
89 })
90 )
91})
92
93
94

Built with git-ssb-web