0.0.0 • Published 7 years ago
pollpoll v0.0.0
PollPoll
PollPoll is a javascript library for asynchronous polling. allowing you do things like periodically check if something in the server has changed.
Usage
PollPoll works by taking in a polling function, with the following signature
() => Promise<{done:boolean, results:any}>
if done
is false
the function will be called again and again until it returns true.results
should contain the results from the call
Example
import PollPoll from "pollpoll";
let poller = (function () {
let count = 0;
return function () {
// simulate a http call using
// a promise that takes sometime
// to resolve
return new Promise(resolve => {
if (count === 10) {
resolve({ done: true, results: { count } });
return;
}
setTimeout(x => {
resolve({ done: false, results: { count } });
++count;
}, 1000);
});
};
})();
let poll = new PollPoll(poller, 100);
poll.events.on("again", (r) => {
console.log(r);
});
async function test() {
try {
console.log("start");
// poll.exec returns a promise that will only resolve if the poller returns {done: true}
await poll.exec(); // make many calls to the server, until something changes
console.log("end");
}
catch (err) {
console.error(err);
}
}
test()
0.0.0
7 years ago