What is a "thunk"?

Thunk is a function that wraps another function or expression to delay computation. Thunk can also be used to add additional calculations to a function.
Thunks provide lazy evaluation and are widely used in functional programming languages.

If you are looking for Redux Thunk, check out this shot.

Example

Here’s an example of using a thunk myFunction to delay evaluation of an expression in JavaScript:

// calculation of 1 + 2 is immediate
let x = 1 + 2;
 
// calculation of 1 + 2 is delayed
// myFunction can be called later to perform the calculation
let myFunction = () => 1 + 2;

Applications

  • Functional programming and lazy evaluation
  • Inheritance of multiple interfaces in object-oriented programming
  • Interoperability between software modules

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved