(condition, options = kEmptyObject)
| 435 | } |
| 436 | |
| 437 | waitFor(condition, options = kEmptyObject) { |
| 438 | validateFunction(condition, 'condition'); |
| 439 | validateObject(options, 'options'); |
| 440 | |
| 441 | const { |
| 442 | interval = 50, |
| 443 | timeout = 1000, |
| 444 | } = options; |
| 445 | |
| 446 | validateNumber(interval, 'options.interval', 0, TIMEOUT_MAX); |
| 447 | validateNumber(timeout, 'options.timeout', 0, TIMEOUT_MAX); |
| 448 | |
| 449 | const { promise, resolve, reject } = PromiseWithResolvers(); |
| 450 | const noError = Symbol(); |
| 451 | let cause = noError; |
| 452 | let pollerId; |
| 453 | let timeoutId; |
| 454 | const done = (err, result) => { |
| 455 | clearTimeout(pollerId); |
| 456 | clearTimeout(timeoutId); |
| 457 | |
| 458 | if (err === noError) { |
| 459 | resolve(result); |
| 460 | } else { |
| 461 | reject(err); |
| 462 | } |
| 463 | }; |
| 464 | |
| 465 | timeoutId = setTimeout(() => { |
| 466 | // eslint-disable-next-line no-restricted-syntax |
| 467 | const err = new Error('waitFor() timed out'); |
| 468 | |
| 469 | if (cause !== noError) { |
| 470 | err.cause = cause; |
| 471 | } |
| 472 | |
| 473 | done(err); |
| 474 | }, timeout); |
| 475 | |
| 476 | const poller = async () => { |
| 477 | try { |
| 478 | const result = await condition(); |
| 479 | |
| 480 | done(noError, result); |
| 481 | } catch (err) { |
| 482 | cause = err; |
| 483 | pollerId = setTimeout(poller, interval); |
| 484 | } |
| 485 | }; |
| 486 | |
| 487 | poller(); |
| 488 | return promise; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | class SuiteContext { |
no test coverage detected