Watched some videos giving an overview of asynchronous JavaScript, all taught by Kyle Simpson. What’s a thunk?

“Time is the most complex factor of state in your program.”

A thunk is simply a function that uses closure to keep track of some value(s) and returns them when the thunk is called.

The code snippet below is a synchronous thunk. It feeds arguments to babyRage() and returns it. Every single time we call thunk(), it will always give the same legitamate reasons for what causes rage.

function babyRage(x, y) {
  return x + ' and ' + y;

var thunk = function() {
  return babyRage("I have a jungle LC", "and no support");

thunk(); // "I have a jungle LC and and no support"

Next is an asynchronous thunk. This thunk takes a callback, gives a couple reasons for rage, and hands babyRageAsync() the callback. Same as our first thunk, except we must pass a callback to our thunk to retrive its value.

function babyRageAsync(x, y, cb) {
  setTimeout(function() {
    var reasons = x + ' and ' + y;
  }, 1000);

var thunk = function(cb) {
  return babyRageAsync("I have a jungle LC", "and no support", cb);

thunk(function(rage) {
  console.log(rage); // "I have a jungle LC and and no support"

What is the significance? TO BE CONTINUED!