1. async function poll(fn, validate, interval = 2500) {
    2. const resolver = async (resolve, reject) => {
    3. try { // catch any error thrown by the "fn" function
    4. const result = await fn(); // fn does not need to be asynchronous or return promise
    5. // call validator to see if the data is at the state to stop the polling
    6. const valid = validate(result);
    7. if (valid === true) {
    8. resolve(result);
    9. } else if (valid === false) {
    10. setTimeout(resolver, interval, resolve, reject);
    11. } // if validator returns anything other than "true" or "false" it stops polling
    12. } catch (e) {
    13. reject(e);
    14. }
    15. };
    16. return new Promise(resolver);
    17. }