Waiting without sleeps: waitFor

A code exercise of about 20 minutes: read the statement, solve «Waiting without sleeps: waitFor» in the browser and get a review like in a real interview.

Code · 20 min
Loading…

Statement

End-to-end tests that wait with sleep(2000) are slow and flaky. Write waitFor(condition, { timeout, interval }): call condition() (it may be async) until it returns a truthy value and resolve with that value. If condition throws, it counts as “not yet” and you keep trying. Wait interval ms between attempts, but never past the timeout. When time runs out, reject with an Error whose message is Timed out after <timeout>ms and, if the last attempt threw, append : <that error's message>.