Skip to content
This repository has been archived by the owner on Jul 28, 2022. It is now read-only.

Commit

Permalink
PascalCase for Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
Sleitnick committed Aug 27, 2020
1 parent fe1e332 commit b3cb92b
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/ReplicatedStorage/Aero/Shared/Promise.lua
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,11 @@ function Promise.defer(callback)

return promise
end
Promise.Defer = Promise.defer

-- Backwards compatibility
Promise.async = Promise.defer
Promise.Async = Promise.defer

--[[
Create a promise that represents the immediately resolved value.
Expand All @@ -316,6 +318,7 @@ function Promise.resolve(...)
resolve(unpack(values, 1, length))
end)
end
Promise.Resolve = Promise.resolve

--[[
Create a promise that represents the immediately rejected value.
Expand All @@ -326,6 +329,7 @@ function Promise.reject(...)
reject(unpack(values, 1, length))
end)
end
Promise.Reject = Promise.reject

--[[
Runs a non-promise-returning function as a Promise with the
Expand All @@ -345,6 +349,7 @@ end
function Promise.try(...)
return Promise._try(debug.traceback(nil, 2), ...)
end
Promise.Try = Promise.try

--[[
Returns a new promise that:
Expand Down Expand Up @@ -438,18 +443,21 @@ end
function Promise.all(promises)
return Promise._all(debug.traceback(nil, 2), promises)
end
Promise.All = Promise.all

function Promise.some(promises, amount)
assert(type(amount) == "number", "Bad argument #2 to Promise.some: must be a number")

return Promise._all(debug.traceback(nil, 2), promises, amount)
end
Promise.Some = Promise.some

function Promise.any(promises)
return Promise._all(debug.traceback(nil, 2), promises, 1):andThen(function(values)
return values[1]
end)
end
Promise.Any = Promise.any

function Promise.allSettled(promises)
if type(promises) ~= "table" then
Expand Down Expand Up @@ -506,6 +514,7 @@ function Promise.allSettled(promises)
end
end)
end
Promise.AllSettled = Promise.allSettled

--[[
Races a set of Promises and returns the first one that resolves,
Expand Down Expand Up @@ -549,6 +558,7 @@ function Promise.race(promises)
end
end)
end
Promise.Race = Promise.race

--[[
Iterates serially over the given an array of values, calling the predicate callback on each before continuing.
Expand Down Expand Up @@ -650,6 +660,7 @@ function Promise.each(list, predicate)
resolve(results)
end)
end
Promise.Each = Promise.each

--[[
Is the given object a Promise instance?
Expand Down Expand Up @@ -678,6 +689,7 @@ function Promise.is(object)

return false
end
Promise.Is = Promise.is

--[[
Converts a yielding function into a Promise-returning one.
Expand All @@ -687,6 +699,7 @@ function Promise.promisify(callback)
return Promise._try(debug.traceback(nil, 2), callback, ...)
end
end
Promise.Promisify = Promise.promisify

--[[
Creates a Promise that resolves after given number of seconds.
Expand Down Expand Up @@ -787,6 +800,7 @@ do
end)
end)
end
Promise.Delay = Promise.delay
end

--[[
Expand All @@ -810,10 +824,12 @@ function Promise.prototype:timeout(seconds, rejectionValue)
self,
})
end
Promise.prototype.Timeout = Promise.prototype.timeout

function Promise.prototype:getStatus()
return self._status
end
Promise.prototype.GetStatus = Promise.prototype.getStatus

--[[
Creates a new promise that receives the result of this promise.
Expand Down Expand Up @@ -882,6 +898,8 @@ function Promise.prototype:andThen(successHandler, failureHandler)

return self:_andThen(debug.traceback(nil, 2), successHandler, failureHandler)
end
Promise.prototype.AndThen = Promise.prototype.andThen
Promise.prototype.Then = Promise.prototype.andThen

--[[
Used to catch any errors that may have occurred in the promise.
Expand All @@ -893,6 +911,7 @@ function Promise.prototype:catch(failureCallback)
)
return self:_andThen(debug.traceback(nil, 2), nil, failureCallback)
end
Promise.prototype.Catch = Promise.prototype.catch

--[[
Like andThen, but the value passed into the handler is also the
Expand All @@ -913,6 +932,7 @@ function Promise.prototype:tap(tapCallback)
return ...
end)
end
Promise.prototype.Tap = Promise.prototype.tap

--[[
Calls a callback on `andThen` with specific arguments.
Expand All @@ -924,6 +944,8 @@ function Promise.prototype:andThenCall(callback, ...)
return callback(unpack(values, 1, length))
end)
end
Promise.prototype.AndThenCall = Promise.prototype.andThenCall
Promise.prototype.ThenCall = Promise.prototype.andThenCall

--[[
Shorthand for an andThen handler that returns the given value.
Expand All @@ -934,6 +956,8 @@ function Promise.prototype:andThenReturn(...)
return unpack(values, 1, length)
end)
end
Promise.prototype.AndThenReturn = Promise.prototype.andThenReturn
Promise.prototype.ThenReturn = Promise.prototype.andThenReturn

--[[
Cancels the promise, disallowing it from rejecting or resolving, and calls
Expand All @@ -960,6 +984,7 @@ function Promise.prototype:cancel()

self:_finalize()
end
Promise.prototype.Cancel = Promise.prototype.cancel

--[[
Used to decrease the number of consumers by 1, and if there are no more,
Expand Down Expand Up @@ -1026,6 +1051,7 @@ function Promise.prototype:finally(finallyHandler)
)
return self:_finally(debug.traceback(nil, 2), finallyHandler)
end
Promise.prototype.Finally = Promise.prototype.finally

--[[
Calls a callback on `finally` with specific arguments.
Expand All @@ -1037,6 +1063,7 @@ function Promise.prototype:finallyCall(callback, ...)
return callback(unpack(values, 1, length))
end)
end
Promise.prototype.FinallyCall = Promise.prototype.finallyCall

--[[
Shorthand for a finally handler that returns the given value.
Expand All @@ -1047,6 +1074,7 @@ function Promise.prototype:finallyReturn(...)
return unpack(values, 1, length)
end)
end
Promise.prototype.FinallyReturn = Promise.prototype.finallyReturn

--[[
Similar to finally, except rejections are propagated through it.
Expand All @@ -1058,6 +1086,7 @@ function Promise.prototype:done(finallyHandler)
)
return self:_finally(debug.traceback(nil, 2), finallyHandler, true)
end
Promise.prototype.Done = Promise.prototype.done

--[[
Calls a callback on `done` with specific arguments.
Expand All @@ -1069,6 +1098,7 @@ function Promise.prototype:doneCall(callback, ...)
return callback(unpack(values, 1, length))
end, true)
end
Promise.prototype.DoneCall = Promise.prototype.doneCall

--[[
Shorthand for a done handler that returns the given value.
Expand All @@ -1079,6 +1109,7 @@ function Promise.prototype:doneReturn(...)
return unpack(values, 1, length)
end, true)
end
Promise.prototype.DoneReturn = Promise.prototype.doneReturn

--[[
Yield until the promise is completed.
Expand Down Expand Up @@ -1107,6 +1138,7 @@ function Promise.prototype:awaitStatus()

return self._status
end
Promise.prototype.AwaitStatus = Promise.prototype.awaitStatus

local function awaitHelper(status, ...)
return status == Promise.Status.Resolved, ...
Expand All @@ -1118,6 +1150,7 @@ end
function Promise.prototype:await()
return awaitHelper(self:awaitStatus())
end
Promise.prototype.Await = Promise.prototype.await

local function expectHelper(status, ...)
if status ~= Promise.Status.Resolved then
Expand All @@ -1134,9 +1167,11 @@ end
function Promise.prototype:expect()
return expectHelper(self:awaitStatus())
end
Promise.prototype.Expect = Promise.prototype.expect

-- Backwards compatibility
Promise.prototype.awaitValue = Promise.prototype.expect
Promise.prototype.AwaitValue = Promise.prototype.expect

--[[
Intended for use in tests.
Expand Down Expand Up @@ -1321,6 +1356,7 @@ function Promise.prototype:now(rejectionValue)
}) or rejectionValue)
end
end
Promise.prototype.Now = Promise.prototype.now

--[[
Retries a Promise-returning callback N times until it succeeds.
Expand All @@ -1339,6 +1375,7 @@ function Promise.retry(callback, times, ...)
end
end)
end
Promise.prototype.Retry = Promise.prototype.retry

--[[
Converts an event into a Promise with an optional predicate
Expand Down Expand Up @@ -1386,5 +1423,6 @@ function Promise.fromEvent(event, predicate)
end)
end)
end
Promise.FromEvent = Promise.fromEvent

return Promise

0 comments on commit b3cb92b

Please sign in to comment.