poll until response
npm i pollize -S
import poll from 'pollize';
// for commonjs -> const pool = require('pollize').default
async function toCheck(i: number): Promise<number> {
await sleep(200);
const val = Math.random();
console.log('got:', val, 'at', new Date());
return i * val;
}
const result = await poll(toCheck,{
onFulfilled: (result) => result > 8, // polling until return more than 8
onReject: (result) => result < 0.5, // throw error once result less than 0.5
}, 10);
console.log('result:', result);
const defaultPollOptions = {
delay: 100, // delay time to start loop query
interval: 1000, // execute interval
timeout: 10 * 1000, // time after a long time and quit loop
};
examples are listed at examples
- [ ] xx
MIT