Write delay(ms, value), the classic sleep helper. It returns a promise that fulfills with value once ms milliseconds have passed. Without a value, it fulfills with undefined. It never rejects.
Even delay(0) waits for a timer, like setTimeout(fn, 0): the promise doesn't fulfill until that timer fires.
async function countdown() {
console.log(3);
await delay(1000);
console.log(2);
const word = await delay(1000, "go");
console.log(word); // "go", two seconds after 3
}
useFakeTimers();
expect(delay(100)).toBeInstanceOf(Promise);
const clock = useFakeTimers();
let done = false;
delay(100).then(() => {
done = true;
});
await clock.tickAsync(99);
expect(done).toBe(false);
const clock = useFakeTimers();
let done = false;
delay(100).then(() => {
done = true;
});
await clock.tickAsync(100);
expect(done).toBe(true);
const clock = useFakeTimers();
let result;
delay(50, "ready").then((value) => {
result = value;
});
await clock.tickAsync(50);
expect(result).toBe("ready");
const clock = useFakeTimers();
let result = "not yet";
delay(10).then((value) => {
result = value;
});
await clock.tickAsync(10);
expect(result).toBeUndefined();
const clock = useFakeTimers();
const order = [];
delay(300, "c").then((value) => order.push(value));
delay(100, "a").then((value) => order.push(value));
delay(200, "b").then((value) => order.push(value));
await clock.tickAsync(300);
expect(order).toEqual(["a", "b", "c"]);
const clock = useFakeTimers();
let done = false;
delay(0).then(() => {
done = true;
});
await flush();
expect(done).toBe(false);
await clock.tickAsync(0);
expect(done).toBe(true);
Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.
Ctrl + Enter runs the tests (⌘ + Enter on a Mac).
I'm here to help with Sleep. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.