Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. class Callback {
  2. constructor(f) {
  3. this.run = (...args) => {
  4. f(...args)
  5. }
  6.  
  7. this.map = g => new Callback((...args) => {
  8. const nArgs = R.init(args)
  9. const callback = R.last(args)
  10.  
  11. nArgs.push((error, ...args) => {
  12. if(!!error) {
  13. callback(error, null)
  14. } else {
  15. callback(null, g(...args))
  16. }
  17. })
  18.  
  19. f(...nArgs)
  20. })
  21.  
  22. this.bind = g => new Callback((...args) => {
  23. const nArgs = R.init(args)
  24. const callback = R.last(args)
  25.  
  26. nArgs.push((error, ...args) => {
  27. if(!!error) {
  28. callback(error, null)
  29. } else {
  30. g(...args).run(null, callback)
  31. }
  32. })
  33.  
  34. f(...nArgs)
  35. })
  36. }
  37. }
  38.  
  39. new Callback((x, cb) => cb(null, x))
  40. .map(x => x * 3)
  41. .bind(x => new Callback((y, cb) => cb(null, x - 1)))
  42. .run(6, (error, result) => console.log(error, result))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement