a zip code crypto-currency system good for red ONLY

123456789101112131415161718192021222324252627282930
  1. var Nil = {}
  2. module.exports = cache
  3. // cache := (Continuable<T>) => Continuable<T>
  4. function cache(source) {
  5. var _err = Nil
  6. var _value = Nil
  7. var _result = null
  8. var listeners = null
  9. return function continuable(callback) {
  10. if (_err !== Nil || _value !== Nil) {
  11. callback(_err, _value)
  12. } else if (listeners) {
  13. listeners.push(callback)
  14. } else {
  15. listeners = [callback]
  16. _result = source(function (err, value) {
  17. _err = err
  18. _value = value
  19. listeners.forEach(function (l) { l(err, value) })
  20. })
  21. }
  22. return _result
  23. }
  24. }