Last active
August 7, 2017 15:51
-
-
Save hoodwink73/b659997dd1780e941c188c2eebd689d1 to your computer and use it in GitHub Desktop.
wrapper over a state with plain old callbacks
This file contains hidden or 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
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