-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
31 lines (27 loc) · 945 Bytes
/
example.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
import {
promisePoll
} from 'promise-poll';
var ONE_SECOND = 1000;
var TEN_SECONDS = 10000;
function main() {
// Declare a function that returns a Promise. promisePull() takes
// a function that returns a Promose like this one:
var asynchThing = function () {
return new Promise(function (resolve, reject) {
setTimeout(function () {
// Decide to resolve true or false.
// true will stop the polling loop
// false will allow polling to continue
var result = Math.random() > 0.7;
resolve(result);
}, ONE_SECOND);
});
}
var timeout = Date.now() + TEN_SECONDS;
// promisePoll() will do the looping and run the async operation many
// times until it resolves truthy or reaches the timeout
promisePoll(asynchThing, ONE_SECOND, timeout)
.then(function (result) {
console.log('Polling done. Result: ' + result;
});
}