Difference between revisions of "Parallel.waitForAny"

From ComputerCraft Wiki
Jump to: navigation, search
m (Moved to CAT:APIFunctions)
(Removed very misleading incorrect note)
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{Function
 
{{Function
 
|name=parallel.waitForAny
 
|name=parallel.waitForAny
|args= function1, function2, so on
+
|args={{type|function}} function1, {{type|function}} function2, ...
|returns = A number indicating which function completed based on argument order
+
|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 function1 and function2 at the same time. Will broadcast what the User enters and prints what it receives over rednet. Both will stop when either something is printed or the user entered something.
+
|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=function1 = [[function]]()
+
|code= local function receive () -- Define function1.
   [[print]] ([[rednet.receive]]())
+
   [[print]]([[rednet.receive]]())
 
  end
 
  end
  function2 = [[function]]()
+
   
   [[rednet.broadcast]] ([[read]]())
+
local function send () -- Define function2.
 +
   [[rednet.broadcast]]([[read]]())
 
  end
 
  end
  parallel.waitForAny (function1, function2)
+
   
 +
'''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

Grid Redstone.png  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

Grid paper.png  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.