Lessons11. timer: a delayed value
Lessons · 11
11. timer: a delayed value
timer — a delayed start
timer(delay) is the RxJS counterpart of setTimeout. It waits the given number of milliseconds, emits a single value (the number 0), and completes the stream right away.
timer(20)
// waits 20 ms → next(0) → complete()
Virtual time — what it is
If we actually waited real milliseconds, tests would be slow and flaky. So RxJS gives us the TestScheduler — "virtual time," where milliseconds fast-forward instantly. This lesson is the first time we use that kind of check.
Glossary
- TestScheduler — a special RxJS scheduler that lets you test async streams without any real waiting.
- Marble string — a text notation for a stream.
20ms (a|)reads as "after 20 milliseconds, valueaand completion|happen at the same time."
Your task
- Find
const done$ = EMPTY;and replaceEMPTYwithtimer(20). - Nothing else needs changing. The marble check already describes the expected behavior.
Deterministic check
The check runs in TestScheduler virtual time. If your solution is right, the console shows only Test Passed!. Don't add extra console.logs — it'll break the comparison.
Solution spoiler · click to reveal
const { timer } = Rx;
const done$ = timer(20); script.ts
CONSOLE · Console output
Hit Run to see the result...