-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEXAMPLES.txt
More file actions
79 lines (66 loc) · 1.92 KB
/
Copy pathEXAMPLES.txt
File metadata and controls
79 lines (66 loc) · 1.92 KB
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
73
74
75
76
77
78
79
/**
* Have a bunch of promises, run them all in parallel, and chain off the result
*/
const eagerQueue = PromiseBatcher.newEagerQueue();
const listOfPromises = [ ... ];
listOfPromises.map(promise => eagerQueue.queue(() => promise)).forEach(result => {
result.then(stuff => {
// Do something with your stuff
});
});
/**
* Run Promises as they come in, but do not exceed a max amount to keep system responsive
*/
const eagerQueue = PromiseBatcher.newEagerQueue();
button.click(() => {
eagerQueue.queue(() => {
return Promise.resolve(generateRandomNumber());
}).then(randomNumber => {
// Do math or something
});
});
/**
* Run promises 1 at a time, guaranteeing execution order
*/
const serialQueue = PromiseBatcher.newSerialQueue();
const listOfPromises = [ ... ];
listOfPromises.map(promise => serialQueue.queue(() => promise)).forEach(result => {
result.then(stuff => {
// Do something with your stuff
});
});
/**
* Run a bunch of promises, 1 at a time and group the result
*/
new Promise((resolve, reject) => {
const serialQueue = PromiseBatcher.newSerialQueue();
const listOfPromises = [ ... ];
const totalCount = listOfPromises.length;
const successes = [];
const failures = [];
const areWeDoneYet = function (allResolves, allRejects, totalCount) {
return allResolves.length + allRejects.length >= totalCount;
};
for (let promise of listOfPromises) {
serialQueue.queue(() => promise)
.then(result => {
successes.push(result);
if (areWeDoneYet(successes, failures, totalCount)) {
resolve({
successes,
failures
});
}
}).catch(error => {
failures.push(reject);
if (areWeDoneYet(successes, failures, totalCount)) {
resolve({
successes,
failures
});
}
});
}
}).then(({ successes, failures }) => {
// Do something with all the results
})