-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.runkit.js
57 lines (42 loc) · 1.92 KB
/
example.runkit.js
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
var PromisePool = require("promise-debounce-pool");
// create a promise resolver function for
// each of your expensive operations
var expensiveFooOperation = function(resolve, reject) {
setTimeout(function() { resolve('foo data ' + Math.random()) }, 1000);
};
var expensiveBarOperation = function(resolve, reject) {
setTimeout(function() { resolve('bar data ' + Math.random()) }, 1000);
};
// create an instance of the pool
var promisePool = new PromisePool();
// associate each promise resolver to a key in the pool
// Note: the functions are not invoked at this point
promisePool.set('foo', expensiveFooOperation);
promisePool.set('bar', expensiveBarOperation);
// make multiple calls
var fooCall1 = promisePool.get('foo') //starts a new foo promise
.then(function(fooData) {
console.log('fooCall1: ' + fooData);
});
var fooCall2 = promisePool.get('foo') //returns the same pending foo promise (fooCall1)
.then(function(fooData) {
console.log('fooCall2: ' + fooData); //same data from fooCall1
});
var barCall1 = promisePool.get('bar') //waits until fooCall1 promise resolved/rejected then creates a new bar promise
.then(function(barData) {
console.log('barCall1: ' + barData);
});
var barCall2 = promisePool.get('bar') //returns the same pending bar promise (barCall1)
.then(function(barData) {
console.log('barCall2: ' + barData); // same data from barCall1
});
setTimeout(function() {
var fooCall3 = promisePool.get('foo') //starts a new foo promise (since previous fooCall1 promise has now resolved)
.then(function(fooData) {
console.log('fooCall3: ' + fooData); // data from new fooCall3 promise
});
var barCall3 = promisePool.get('bar') //returns the same fooCall1 promise from before, its still pending
.then(function(barData) {
console.log('barCall3: ' + barData); // same data from barCall1
});
}, 1500);