Created
August 3, 2017 05:35
-
-
Save giann/c2ccabfecb06cbdd1982bb36e0de1ba3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Fetch n url in parallel, must be run in a coroutine (co parameter) | |
local fetch = function(co, ...) | |
local requests = {...} | |
for i = 1, #requests do | |
local promise = js.global:fetch(requests[i]) | |
promise["then"](promise, function(_, res) | |
local textPromise = res:text() | |
textPromise["then"](textPromise, function(_, text) | |
co(i, text) | |
end) | |
end) | |
end | |
local results = {} | |
for i = 1, #requests do | |
local requestId, response = coroutine.yield() | |
results[requestId] = response | |
end | |
return table.unpack(results) | |
end | |
-- Take full paths for now | |
local require = function(...) | |
local paths = table.pack(...) | |
local co | |
co = coroutine.wrap(function () | |
local modules = table.pack(fetch(co, table.unpack(paths))) | |
for i = 1, #modules do | |
local module = modules[i] | |
modules[i] = load(module) | |
end | |
coroutine.yield(table.unpack(modules)) | |
end) | |
return co() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment