Created
July 30, 2018 01:49
-
-
Save frankfaustino/13ae5533ef90552e9a198cbfa40c4f19 to your computer and use it in GitHub Desktop.
thunk vs async thunk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* —— 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