Difference between revisions of "Parallel.waitForAny"
From ComputerCraft Wiki
m (Function -> Function (type)) |
(Removed very misleading incorrect note) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
{{Function | {{Function | ||
|name=parallel.waitForAny | |name=parallel.waitForAny | ||
− | |args= function1, function2, | + | |args={{type|function}} function1, {{type|function}} function2, ... |
− | |returns = | + | |returns = {{type|number}} indicating which function has completed based on argument order |
|api=parallel | |api=parallel | ||
|addon=ComputerCraft | |addon=ComputerCraft | ||
− | |desc=Lets you run multiple functions at the same time. | + | |desc=Lets you run multiple functions at the same time, alternating between them until any happen to finish. |
|examples= | |examples= | ||
{{Example | {{Example | ||
− | |desc=Tells the computer to run | + | |desc=Tells the computer to run <var>receive</var >and <var>send</var> the same time. Will broadcast what the user enters or prints what it receives over rednet, depending on what happens first. |
− | |code= | + | |code= local function receive () -- Define function1. |
− | [[print]] ([[rednet.receive]]()) | + | [[print]]([[rednet.receive]]()) |
end | end | ||
− | + | ||
− | [[rednet.broadcast]] ([[read]]()) | + | local function send () -- Define function2. |
+ | [[rednet.broadcast]]([[read]]()) | ||
end | end | ||
− | parallel.waitForAny ( | + | |
+ | '''parallel.waitForAny(receive, send)''' -- Run both functions in parallel until one finishes. | ||
|output=Either it prints what it received via rednet, or it broadcasts the message the user typed. | |output=Either it prints what it received via rednet, or it broadcasts the message the user typed. | ||
}} | }} |
Latest revision as of 21:54, 27 January 2016
Function parallel.waitForAny | |
Lets you run multiple functions at the same time, alternating between them until any happen to finish. | |
Syntax | parallel.waitForAny(function function1, function function2, ...) |
Returns | number indicating which function has completed based on argument order |
Part of | ComputerCraft |
API | parallel |
Examples
Example | |
Tells the computer to run receiveand send the same time. Will broadcast what the user enters or prints what it receives over rednet, depending on what happens first. | |
Code |
local function receive () -- Define function1. print(rednet.receive()) end local function send () -- Define function2. rednet.broadcast(read()) end parallel.waitForAny(receive, send) -- Run both functions in parallel until one finishes. |
Output | Either it prints what it received via rednet, or it broadcasts the message the user typed. |