Pseudo Random Nonsense Generator

thunks

November 22, 2018

this is a think

(i just made this up for comparison)

// JS
function doSomething() {
  console.log('do something in here')
  return 42
}

// JS ES6
const doSomething = () => {
  console.log('do something in here')
  return 42
}

think = function = that normal function call

this is a thunk

// JS
function doSomething() {  return function() {    console.log('do something in here')
    return 42
  }
}

// JS ES6
const doSomething = () => {  return () => {    console.log('do something in here')
    return 42
  }
}

// JS ES6 with even more shorthand bits
const doSomething = () => () => {  console.log('do something in here')
  return 42
}

thunk = it is a function returned by another function

more explanation required

According to Eric Raymond, the inventors of the thunk coined the term “after they realized (in the wee hours after hours of discussion) that the type of an argument in Algol-60 could be figured out in advance with a little compile-time thought […] In other words, it had ‘already been thought of’; thus it was christened a thunk, which is ‘the past tense of “think” at two in the morning”.

@shoshanarosenfield (redux-thunk-vs-redux-saga medium article)

Another good way to look at it ref

// Eager version
function yell(text) {
  console.log(text + '!')
}

yell('bonjour') // 'bonjour!'

// Lazy (or "thunked") version
function thunkedYell(text) {
  return function thunk() {
    console.log(text + '!')
  }
}

const thunk = thunkedYell('bonjour') // no action yet.

// wait for it…
thunk() // 'bonjour!'

Article References