Skip to content

Instantly share code, notes, and snippets.

@frankfaustino
Created July 30, 2018 01:49
Show Gist options
  • Save frankfaustino/13ae5533ef90552e9a198cbfa40c4f19 to your computer and use it in GitHub Desktop.
Save frankfaustino/13ae5533ef90552e9a198cbfa40c4f19 to your computer and use it in GitHub Desktop.
thunk vs async thunk
/* —— thunks
* A nullary function (no arguments) that returns closured value(s).
* Basically a token or wrapper that represents a specific value.
*/
const add = (x, y) => x + y
const thunk = () => add(6, 3)
thunk() // 8
/* —— asynchronous thunks
* A nullary function that when passed a callback function returns a value.
*/
const addAsync = (x, y, cb) => {
setTimeout(() => {
cb(x + y)
}, 1000)
}
const asyncThunk = (cb) => addAsync(6, 2, cb)
asyncThunk(function(value) {
console.log(value) // 8
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment