MCPcopy Index your code
hub / github.com/nodejs/node / waitFor

Method waitFor

lib/internal/test_runner/test.js:437–489  ·  view source on GitHub ↗
(condition, options = kEmptyObject)

Source from the content-addressed store, hash-verified

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
492class SuiteContext {

Calls 3

SymbolFunction · 0.50
setTimeoutFunction · 0.50
doneFunction · 0.50

Tested by

no test coverage detected