@override
(condition, timeout = 0, message = undefined, pollTimeout = 200)
| 846 | |
| 847 | /** @override */ |
| 848 | wait(condition, timeout = 0, message = undefined, pollTimeout = 200) { |
| 849 | if (typeof timeout !== 'number' || timeout < 0) { |
| 850 | throw TypeError('timeout must be a number >= 0: ' + timeout) |
| 851 | } |
| 852 | |
| 853 | if (typeof pollTimeout !== 'number' || pollTimeout < 0) { |
| 854 | throw TypeError('pollTimeout must be a number >= 0: ' + pollTimeout) |
| 855 | } |
| 856 | |
| 857 | if (promise.isPromise(condition)) { |
| 858 | return new Promise((resolve, reject) => { |
| 859 | if (!timeout) { |
| 860 | resolve(condition) |
| 861 | return |
| 862 | } |
| 863 | |
| 864 | let start = Date.now() |
| 865 | let timer = setTimeout(function () { |
| 866 | timer = null |
| 867 | try { |
| 868 | let timeoutMessage = resolveWaitMessage(message) |
| 869 | reject( |
| 870 | new error.TimeoutError( |
| 871 | `${timeoutMessage}Timed out waiting for promise to resolve after ${Date.now() - start}ms`, |
| 872 | ), |
| 873 | ) |
| 874 | } catch (ex) { |
| 875 | reject( |
| 876 | new error.TimeoutError( |
| 877 | `${ex.message}\nTimed out waiting for promise to resolve after ${Date.now() - start}ms`, |
| 878 | ), |
| 879 | ) |
| 880 | } |
| 881 | }, timeout) |
| 882 | const clearTimer = () => timer && clearTimeout(timer) |
| 883 | |
| 884 | /** @type {!IThenable} */ condition.then( |
| 885 | function (value) { |
| 886 | clearTimer() |
| 887 | resolve(value) |
| 888 | }, |
| 889 | function (error) { |
| 890 | clearTimer() |
| 891 | reject(error) |
| 892 | }, |
| 893 | ) |
| 894 | }) |
| 895 | } |
| 896 | |
| 897 | let fn = /** @type {!Function} */ (condition) |
| 898 | if (condition instanceof Condition) { |
| 899 | message = message || condition.description() |
| 900 | fn = condition.fn |
| 901 | } |
| 902 | |
| 903 | if (typeof fn !== 'function') { |
| 904 | throw TypeError('Wait condition must be a promise-like object, function, or a ' + 'Condition object') |
| 905 | } |