HeroBrine1st

MoonScript Promise

Feb 2nd, 2019
110
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. thread = require("thread")
  2.  
  3. class Promise
  4. new: (async) =>
  5. @result = {}
  6. @rejectedHandlers = {}
  7. @fulfilledHandlers = {}
  8. @state = "pending"
  9. handlersCache = 0
  10. @resolve = (a) =>
  11. @result = a
  12. handlers = @fulfilledHandlers
  13. for i = handlersCache+1, #handlers
  14. handlersCache = i
  15. if type(@result) == "table" and @result.__class == @@
  16. result = @result\await()
  17. if @result.state == "rejected"
  18. @reject(@result)
  19. return
  20. @result = result
  21. res = {pcall(handlers[i],@result)}
  22. success = table.remove(res,1)
  23. @result = res[1]
  24. if not success
  25. @reject(@result)
  26. return
  27. @state = "fulfilled"
  28. @reject = (a) =>
  29. @result = a
  30. handlers = @rejectedHandlers
  31. for i = handlersCache+1, #handlers
  32. handlersCache = i
  33. res = {pcall(handlers[i],@result)}
  34. success = table.remove(res,1)
  35. @result = res[1]
  36. if success then
  37. @resolve(@result)
  38. return
  39. @state = "rejected"
  40. @thread = thread.create(() ->
  41. resolve = (...) ->
  42. if @state == "pending"
  43. @resolve(...)
  44. reject = (...) ->
  45. if @state == "pending"
  46. @reject(...)
  47. success, reason = pcall(async,resolve,reject)
  48. if not success
  49. @reject(reason)
  50. )
  51. next: (onResolve,onReject) =>
  52. table.insert(@fulfilledHandlers,onResolve)
  53. if @state =="fulfilled"
  54. @resolve(@result)
  55. if not onReject
  56. onReject = (e) -> error(e)
  57. table.insert(@rejectedHandlers,onReject)
  58. return @
  59. catch: (onReject) =>
  60. table.insert(@rejectedHandlers,onReject)
  61. if @state == "rejected"
  62. @reject(@result)
  63. table.insert(@fulfilledHandlers,(a)->return a)
  64. return @
  65. await: () =>
  66. thread.waitForAny({@thread})
  67. return @result
  68.  
  69. Promise.all = (list) ->
  70. return Promise((resolve, reject) ->
  71. values = {}
  72. while true
  73. os.sleep(0.1)
  74. isFulfilled = true
  75. for i = 1, #list
  76. prom = list[i]
  77. if prom.state == "pending"
  78. isFulfilled = false
  79. elseif prom.state == "rejected"
  80. reject(prom.result)
  81. elseif prom.state == "fulfilled"
  82. table.remove(list,i)
  83. promResult = prom.result
  84. table.insert(values,promResult)
  85. if isFulfilled
  86. resolve(values)
  87. )
  88.  
  89. Promise.race = (list) ->
  90. for i = 1, #list
  91. if not (type(list[i]) == "table" and list[i].__class and list[i].__class.__name == "Promise")
  92. list[i] = Promise.resolve(list[i])
  93. return Promise((resolve,reject) ->
  94. while true do
  95. os.sleep(0.1)
  96. for i = 1, #list
  97. prom = list[i]
  98. if prom.state ~= "pending"
  99. if prom.state == "rejected"
  100. reject(prom.result)
  101. elseif prom.state == "fulfilled"
  102. resolve(prom.result)
  103. )
  104. Promise.resolve = (value) ->
  105. return Promise((a,b) ->
  106. a(value)
  107. )
  108. Promise.reject = (reason) ->
  109. return Promise((a,b)->
  110. b(reason)
  111. )
  112.  
  113. return Promise
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×