Message from JavaScript talks

May 2017

— Thunk?


A thunk is a function which acts as a wrapper to another function, not quite a stored procedure but rather a stored call to another procedure

— Good example is a Promise itself

— A Promise is a specific type called a continuation monad, a type of a thunk used in continuation-passing style

Message permanent page


thunk is a function that encapsulates synchronous or asynchronous code inside.

thunk accepts only one callback function as an arguments, which is a CPS function.

thunk returns another thunk function after being called, for chaining operations.

thunk passes the results into a callback function after being excuted.

If the return value of callback is a thunk function, then it will be executed first and its result will be sent to another thunk for excution, or it will be sent to another new thunk function as the value of the computation.

Message permanent page

— I see

— Welcome

— A nice example of a thunk is an infinite list.

— A computer obviously can't hold an infinitely long list, but if you encapsulate it such that you get the value only when it's needed, then you can perform operations on it.

Message permanent page

— For example, something like take n elements

— Aah

— So...