-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.md
72 lines (50 loc) · 1.93 KB
/
README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# promise-pauser
Promise-pauser is a Node library for pausing multiple promise chains and/or streams with one call. Allows pausing already started promise chains / streams at controllable points, making it easy to halt multiple concurrent processes when a certain network resource goes down.
The end result is similar to RxJS's `Pauser`.
# Examples
Also in the [/examples](/examples) directory.
## With multiple promise chains
```javascript
var pause = require('promise-pauser');
var Promise = require('bluebird');
var pauser = pause.pauser();
pauser.pause();
var chain1 = Promise.resolve([1,2,3,4,5])
.tap(console.log)
.tap(pause.waitFor(pauser))
.map(function(x){return x + 10;})
.tap(console.log);
var chain2 = Promise.resolve([10,20,30,40,50])
.tap(console.log)
.tap(pause.waitFor(pauser))
.map(function(x){return x + 1000;})
.tap(console.log);
console.log("Unpausing in 3 seconds");
setTimeout(pauser.unpause, 3000);
```
## Streams
You can also use promise-pauser to pause multiple [Highland](http://highlandjs.org/) streams with one call.
```javascript
var Promise = require('bluebird');
var $ = require('highland');
var pause = require('promise-pauser');
var pauser = pause.pauser();
var str1 = $([1,2,3,4,5,6,7,8]).ratelimit(1, 500)
.flatMap($.compose($, pause.waitFor(pauser))) // pause _before_ the map
.map($.add(5))
.each($.partial($.log, "str1 end"));
/*
$.compose($, pause.waitFor(pauser)) ==
function(x) {
return $(pause.waitFor(pauser)(x))
}
*/
var str2 = $([10,20,30,40,50]).ratelimit(1, 500)
.map($.add(100))
.doto($.partial($.log, "after str2 map"))
.flatMap($.compose($, pause.waitFor(pauser))) // note that the pause is
// _after_ the map
.each($.partial($.log, "str2 end"));
setTimeout($.compose(pauser.pause, $.partial($.log, "\npaused\n")), 1050);
setTimeout($.compose(pauser.unpause, $.partial($.log, "\nunpause\n")), 3000);
```