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