Thread
Fork of luau-thread for Roblox-ts
The module must first export a function:
// src/shared/module
export = function(timeToWait: number) {
task.wait(timeToWait);
print("waited", timeToWait, "second(s)");
}
Then, to get our module, we can use the new $getModuleTree macro, and the function provided in this library
import Thread from "@rbxts/luau-thread";
const [root, parts] = $getModuleTree("shared/module");
const module = Thread.getModuleByTree(root, parts);
Afterwards, we can spawn the module and wait for it to finish:
const identifier = Thread.spawn(module, 1);
Thread.join(identifier);
// prints: waited 1 second(s)
We can also spawn it multiple times, and wait for all threads to finish:
const identifiers = [];
for (const i of $range(1, 10)) {
identifiers.push(Thread.spawn(module, i));
}
Thread.join(identifiers);
// prints: waited 1 second(s)
// ...
// prints: waited 10 seconds(s)