mirror of
https://github.com/AmberGraceRblx/luau-promise.git
synced 2025-04-24 15:50:01 +00:00
Rename, implement Promise.all
This commit is contained in:
parent
d9bd6bbd3c
commit
8b1573ce41
3 changed files with 34 additions and 8 deletions
|
@ -4,8 +4,8 @@
|
|||
|
||||
local PROMISE_DEBUG = false
|
||||
|
||||
-- If promise debugging is on, use a version of pcall that warns on failure.
|
||||
-- This is useful for finding errors that happen within Promise itself.
|
||||
-- If promise debugging is on, use a version of pcall that warns on failure. This is useful for finding errors that
|
||||
-- happen within Promise itself.
|
||||
local wpcall
|
||||
if PROMISE_DEBUG then
|
||||
wpcall = function(f, ...)
|
||||
|
@ -71,8 +71,8 @@ Promise.Status = {
|
|||
end
|
||||
|
||||
get("https://google.com")
|
||||
:andThen(function(stuff)
|
||||
print("Got some stuff!", stuff)
|
||||
:andThen(function(body)
|
||||
print("Got a body:", body)
|
||||
end)
|
||||
]]
|
||||
function Promise.new(callback)
|
||||
|
@ -139,8 +139,33 @@ end
|
|||
* is resolved when all input promises resolve
|
||||
* is rejected if ANY input promises reject
|
||||
]]
|
||||
function Promise.all(...)
|
||||
error("unimplemented", 2)
|
||||
function Promise.all(promises)
|
||||
return Promise.new(function(resolve, reject)
|
||||
local results = {}
|
||||
local totalCount = #promises
|
||||
local finishedCount = 0
|
||||
local rejected = false
|
||||
|
||||
local function rejectedHandler(value)
|
||||
if rejected then
|
||||
return
|
||||
end
|
||||
|
||||
rejected = true
|
||||
reject(value)
|
||||
end
|
||||
|
||||
for index, promise in ipairs(promises) do
|
||||
promise:andThen(function(value)
|
||||
results[index] = value
|
||||
finishedCount = finishedCount + 1
|
||||
|
||||
if finishedCount == totalCount then
|
||||
resolve(results)
|
||||
end
|
||||
end, rejectedHandler)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
--[[
|
1
lib/Promise.spec.lua
Normal file
1
lib/Promise.spec.lua
Normal file
|
@ -0,0 +1 @@
|
|||
local Promise = require(script.Parent.Promise)
|
|
@ -3,8 +3,8 @@
|
|||
"servePort": 8000,
|
||||
"partitions": {
|
||||
"lib": {
|
||||
"path": "src",
|
||||
"target": "ServerScriptService.roblox-request"
|
||||
"path": "lib",
|
||||
"target": "ReplicatedStorage.lua-promise"
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue