Skip to content

Instantly share code, notes, and snippets.

@hoodwink73
Last active August 7, 2017 15:51
Show Gist options
  • Save hoodwink73/b659997dd1780e941c188c2eebd689d1 to your computer and use it in GitHub Desktop.
Save hoodwink73/b659997dd1780e941c188c2eebd689d1 to your computer and use it in GitHub Desktop.
wrapper over a state with plain old callbacks
function addAsync (getX, getY) {
var x, y, consumer;
getX(function (xVal) {
if (y !== undefined && consumer !== undefined) {
consumer (xVal + y)
}.else {
x = xVal
}
})
getY(function (yVal) {
if (x !== undefined && consumer !== undefined) {
consumer (x + yVal)
} else {
y = yVal
}
})
return function (doSomethingWithSum) {
if (x !== undefined && y !== undefined) {
doSomethingWithSum(x + y);
} else {
consumer = doSomethingWithSum;
}
}
}
var printSum = function (sum) {
console.log(sum)
}
var sumPlaceholder = addAsync(fetchX, fetchY);
sumPlaceholder(printSum);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment