Advertisement
HeroBrine1st

Lua Promise

Feb 2nd, 2019
1,306
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local thread = require("thread")
  2. local Promise
  3. do
  4.   local _class_0
  5.   local _base_0 = {
  6.     next = function(self, onResolve, onReject)
  7.       table.insert(self.fulfilledHandlers, onResolve)
  8.       if self.state == "fulfilled" then
  9.         self:resolve(self.result)
  10.       end
  11.       if not onReject then
  12.         onReject = function(e)
  13.           return error(e)
  14.         end
  15.       end
  16.       table.insert(self.rejectedHandlers, onReject)
  17.       return self
  18.     end,
  19.     catch = function(self, onReject)
  20.       table.insert(self.rejectedHandlers, onReject)
  21.       if self.state == "rejected" then
  22.         self:reject(self.result)
  23.       end
  24.       table.insert(self.fulfilledHandlers, function(a)
  25.         return a
  26.       end)
  27.       return self
  28.     end,
  29.     await = function(self)
  30.       thread.waitForAny({
  31.         self.thread
  32.       })
  33.       return self.result
  34.     end
  35.   }
  36.   _base_0.__index = _base_0
  37.   _class_0 = setmetatable({
  38.     __init = function(self, async)
  39.       self.result = { }
  40.       self.rejectedHandlers = { }
  41.       self.fulfilledHandlers = { }
  42.       self.state = "pending"
  43.       local handlersCache = 0
  44.       self.resolve = function(self, a)
  45.         self.result = a
  46.         local handlers = self.fulfilledHandlers
  47.         for i = handlersCache + 1, #handlers do
  48.           handlersCache = i
  49.           if type(self.result) == "table" and self.result.__class == self.__class then
  50.             local result = self.result:await()
  51.             if self.result.state == "rejected" then
  52.               self:reject(self.result)
  53.               return
  54.             end
  55.             self.result = result
  56.           end
  57.           local res = {
  58.             pcall(handlers[i], self.result)
  59.           }
  60.           local success = table.remove(res, 1)
  61.           self.result = res[1]
  62.           if not success then
  63.             self:reject(self.result)
  64.             return
  65.           end
  66.         end
  67.         self.state = "fulfilled"
  68.       end
  69.       self.reject = function(self, a)
  70.         self.result = a
  71.         local handlers = self.rejectedHandlers
  72.         for i = handlersCache + 1, #handlers do
  73.           handlersCache = i
  74.           local res = {
  75.             pcall(handlers[i], self.result)
  76.           }
  77.           local success = table.remove(res, 1)
  78.           self.result = res[1]
  79.           if success then
  80.             self:resolve(self.result)
  81.             return
  82.           end
  83.         end
  84.         self.state = "rejected"
  85.       end
  86.       self.thread = thread.create(function()
  87.         local resolve
  88.         resolve = function(...)
  89.           if self.state == "pending" then
  90.             return self:resolve(...)
  91.           end
  92.         end
  93.         local reject
  94.         reject = function(...)
  95.           if self.state == "pending" then
  96.             return self:reject(...)
  97.           end
  98.         end
  99.         local success, reason = pcall(async, resolve, reject)
  100.         if not success then
  101.           return self:reject(reason)
  102.         end
  103.       end)
  104.     end,
  105.     __base = _base_0,
  106.     __name = "Promise"
  107.   }, {
  108.     __index = _base_0,
  109.     __call = function(cls, ...)
  110.       local _self_0 = setmetatable({}, _base_0)
  111.       cls.__init(_self_0, ...)
  112.       return _self_0
  113.     end
  114.   })
  115.   _base_0.__class = _class_0
  116.   Promise = _class_0
  117. end
  118. Promise.all = function(list)
  119.   return Promise(function(resolve, reject)
  120.     local values = { }
  121.     while true do
  122.       os.sleep(0.1)
  123.       local isFulfilled = true
  124.       for i = 1, #list do
  125.         local prom = list[i]
  126.         if prom.state == "pending" then
  127.           isFulfilled = false
  128.         elseif prom.state == "rejected" then
  129.           reject(prom.result)
  130.         elseif prom.state == "fulfilled" then
  131.           table.remove(list, i)
  132.           local promResult = prom.result
  133.           table.insert(values, promResult)
  134.         end
  135.       end
  136.       if isFulfilled then
  137.         resolve(values)
  138.       end
  139.     end
  140.   end)
  141. end
  142. Promise.race = function(list)
  143.   for i = 1, #list do
  144.     if not (type(list[i]) == "table" and list[i].__class and list[i].__class.__name == "Promise") then
  145.       list[i] = Promise.resolve(list[i])
  146.     end
  147.   end
  148.   return Promise(function(resolve, reject)
  149.     while true do
  150.       os.sleep(0.1)
  151.       for i = 1, #list do
  152.         local prom = list[i]
  153.         if prom.state ~= "pending" then
  154.           if prom.state == "rejected" then
  155.             reject(prom.result)
  156.           elseif prom.state == "fulfilled" then
  157.             resolve(prom.result)
  158.           end
  159.         end
  160.       end
  161.     end
  162.   end)
  163. end
  164. Promise.resolve = function(value)
  165.   return Promise(function(a, b)
  166.     return a(value)
  167.   end)
  168. end
  169. Promise.reject = function(reason)
  170.   return Promise(function(a, b)
  171.     return b(reason)
  172.   end)
  173. end
  174. return Promise
Advertisement
RAW Paste Data Copied
Advertisement