Created
February 17, 2017 19:19
-
-
Save stvedt/a669d98c6a54c343bbd9ed14d900b5e2 to your computer and use it in GitHub Desktop.
Thunks
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
//synchronous thunk | |
function add(x,y){ | |
return x + y; | |
} | |
//wrapper to return a value | |
var thunk = function(){ | |
return add(10,15); | |
} | |
thunk(); | |
//Asynchronous thunks don't take any values but rather a callback | |
function addAsync(x,y,cb) { | |
setTimeout(function(){ | |
return cb( x + y ); | |
}, 1000); | |
} | |
var thunk = function(cb){ | |
addAsync(10,15,cb); | |
} | |
thunk(function (sum){ | |
console.log(sum); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment