Refactor waitUntil' and waitWhile'