delay & sleep
Wait for a bit, then produce a value (delay) — or just wait (sleep).
Lecture
delay(wait, value) returns a Future that
completes with value after wait has elapsed.
sleep(wait) is the same idea with nothing to return — it
just completes after the duration. Both are thin wrappers over
Future.delayed, and both are exactly what you'd reach for to
simulate a slow operation: a network call, a database query, anything
that takes time and eventually produces (or doesn't produce) a result.
These two functions are the backbone of nearly every async demo across
this whole tutorial site. Whenever you see mapAsync,
concurrent, debounce, or throttle
demonstrated with a Stopwatch proving something ran in
parallel or was rate-limited, it's delay or sleep
doing the actual waiting under the hood.
Demo 1 · Basics
Demo 2 · Simulating concurrent work
Three "requests" each take 150ms, but run concurrently — the whole batch still finishes in well under 450ms:
Try it yourself
Exercise: use delay to simulate a 100ms "fetch" that
returns 'pong'.
memoize — caches a delayed Future so it only waits once ·
concurrent — run multiple delays in parallel ·
debounce / throttle — rate-limiting built on top of waiting ·
toAsync — turn a plain Iterable into an async pipeline