Home

Thunks

A thunk is a small routine that encapsulates a computation to be performed later. In programming, a thunk is typically a parameterless function or closure that delays evaluation of an expression until it is explicitly invoked.

Thunks serve several roles: they implement lazy evaluation, control evaluation order, or adapt calling conventions. In

In practical programming, thunks appear in many forms. In JavaScript, a thunk is a function that wraps

Example: a thunk can be defined as function thunk() { return expensiveComputation(); }. Calling thunk() executes the computation

The term likely originated in early compiler literature, referring to a small piece of code that defers

call-by-name
languages,
a
thunk
suspends
a
computation
for
later
evaluation;
in
call-by-need,
the
result
is
memoized
after
the
first
use.
By
delaying
execution,
thunks
can
help
avoid
unnecessary
work
and
enable
more
flexible
evaluation
strategies.
a
computation
and
returns
its
result
when
called.
Middleware
such
as
redux-thunk
enables
action
creators
to
return
a
function
(instead
of
a
plain
action)
that
receives
dispatch
and
getState
to
perform
asynchronous
or
conditional
work.
at
that
moment.
Thunks
are
also
used
by
compilers
to
implement
tail
calls
or
to
suspend
evaluation
of
expressions
for
optimization,
and
they
appear
in
various
functional
languages
as
a
standard
mechanism
to
defer
work
and
manage
side
effects.
a
computation.
The
precise
origin
is
not
undisputed.
Across
languages,
thunks
remain
a
fundamental
concept
for
controlling
when
and
how
expressions
are
evaluated,
and
for
enabling
lazy
or
asynchronous
programming
patterns.