Rename, implement Promise.all

This commit is contained in:
Lucien Greathouse 2018-01-31 13:43:09 -08:00
parent d9bd6bbd3c
commit 8b1573ce41
3 changed files with 34 additions and 8 deletions

View file

@ -4,8 +4,8 @@
local PROMISE_DEBUG = false local PROMISE_DEBUG = false
-- If promise debugging is on, use a version of pcall that warns on failure. -- If promise debugging is on, use a version of pcall that warns on failure. This is useful for finding errors that
-- This is useful for finding errors that happen within Promise itself. -- happen within Promise itself.
local wpcall local wpcall
if PROMISE_DEBUG then if PROMISE_DEBUG then
wpcall = function(f, ...) wpcall = function(f, ...)
@ -71,8 +71,8 @@ Promise.Status = {
end end
get("https://google.com") get("https://google.com")
:andThen(function(stuff) :andThen(function(body)
print("Got some stuff!", stuff) print("Got a body:", body)
end) end)
]] ]]
function Promise.new(callback) function Promise.new(callback)
@ -139,8 +139,33 @@ end
* is resolved when all input promises resolve * is resolved when all input promises resolve
* is rejected if ANY input promises reject * is rejected if ANY input promises reject
]] ]]
function Promise.all(...) function Promise.all(promises)
error("unimplemented", 2) 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 end
--[[ --[[

1
lib/Promise.spec.lua Normal file
View file

@ -0,0 +1 @@
local Promise = require(script.Parent.Promise)

View file

@ -3,8 +3,8 @@
"servePort": 8000, "servePort": 8000,
"partitions": { "partitions": {
"lib": { "lib": {
"path": "src", "path": "lib",
"target": "ServerScriptService.roblox-request" "target": "ReplicatedStorage.lua-promise"
} }
} }
} }