* Waits for specified function to resolve to a truthy value. * * @param {Function} fn function to be evaluated until truthy * @param {*} arg optional argument to pass to `fn` * @param {Object} options optional parameters * @returns {Promise} promise which resolves to function result
(fn, arg, options = {})
| 12 | * @returns {Promise} promise which resolves to function result |
| 13 | */ |
| 14 | function waitForFunction(fn, arg, options = {}) { |
| 15 | const settings = { |
| 16 | ...defaults, |
| 17 | ...options, |
| 18 | }; |
| 19 | |
| 20 | return new Promise((resolve, reject) => { |
| 21 | let timeElapsed = 0; |
| 22 | |
| 23 | const int = setInterval(() => { |
| 24 | let result; |
| 25 | |
| 26 | try { |
| 27 | result = fn(arg); |
| 28 | } catch (e) { |
| 29 | // Continue... |
| 30 | } |
| 31 | |
| 32 | if (result) { |
| 33 | clearInterval(int); |
| 34 | resolve(result); |
| 35 | } |
| 36 | |
| 37 | timeElapsed += settings.delay; |
| 38 | |
| 39 | if (timeElapsed >= settings.timeout) { |
| 40 | const msg = `waitForFunction did not return a truthy value (${ |
| 41 | settings.timeout |
| 42 | } ms): ${fn.toString()}\n`; |
| 43 | |
| 44 | reject(msg); |
| 45 | } |
| 46 | }, settings.delay); |
| 47 | }); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Waits for specified CSS selector to be located in the DOM |
no outgoing calls
no test coverage detected
searching dependent graphs…