fetchNext — Return the next completed unread future result.
fetchNext selects a completed unread future by completion order and returns its one-based index followed by requested outputs.
Syntax
[index, outputs...] = fetchNext(futures)
[index, outputs...] = fetchNext(futures, timeout)Inputs
futures- Type
Any- Required
- Yes
- Default
- —
- Description
- Future array from which to retrieve the next unread result.
timeout- Type
NumericScalar- Required
- Yes
- Default
- —
- Description
- Maximum number of seconds to wait for an unread result.
Returns
index- Type
NumericScalar- Description
- Linear index of the completed future.
outputs- Type
Any- Description
- Outputs produced by the selected future.
Returned values from fetchNext depend on how many outputs the caller requests.
Errors
InvalidFutureArray- When
- The first argument is not a parallel future scalar or array.
- Message
fetchNext expects a parallel.FevalFuture scalar or array
NoUnreadFutures- When
- The future array has no unread results.
- Message
fetchNext found no unread futures
OutputCountMismatch- When
- The caller's requested output count does not match the selected future.
- Message
fetchNext output count must match the selected future
InvalidTimeout- When
- The timeout is not a finite nonnegative real scalar.
- Message
fetchNext timeout must be a finite nonnegative real scalar
Behavior
Fetching waits through the session's async execution service. A single future returns the scheduled callable's requested outputs and structured errors; fetching several worker results assembles each output by vertical concatenation by default.
Future arrays retain one-based source indices. fetchNext marks one completed result as read; a timeout can return [] in every requested output slot. fetchOutputs(..., "UniformOutput", false) returns cell arrays instead of concatenating multiple results.
Execution placement
Fetch operations cross the worker-to-client result boundary. Numeric placement after transfer follows the scheduled callable and result-assembly contract rather than a provider rule owned by the fetch builtin.
Example
Read completed futures one at a time
pool = parpool(2);
first = parfeval(pool, @(x) x + 1, 1, 4);
second = parfeval(pool, @(x) x + 2, 1, 8);
futures = first; futures(2) = second;
[index, value] = fetchNext(futures)Expected output:
index identifies the completed future and value is its resultUsing fetchNext with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how fetchNext changes the result.
Run a small fetchNext example, explain the result, then change one input and compare the output.
FAQ
Does fetching change output classes?⌄
A single future returns its callable's output. Fetching multiple results concatenates them by default; UniformOutput, false instead returns cells.
Can fetching suspend?⌄
Yes. It waits through the session async runtime until a matching result, timeout, cancellation, or failure is available.
What does UniformOutput control?⌄
For future arrays, false preserves each result in a cell instead of concatenating compatible values.
Can the same result be returned twice by fetchNext?⌄
No. A selected future is marked read before the next completion-order selection.
Related Parallel functions
codistributed · codistributed.build · codistributor · codistributor1d · codistributor2dbc · distributed · fetchOutputs · gcat · gcp · getCodistributor · getCurrentJob · getCurrentTask · getCurrentWorker · getLocalPart · globalIndices · gop · gplus · iscodistributed · isComplete · labBarrier · labBroadcast · labindex · labProbe · labReceive · labSend · labSendReceive · numlabs · parfeval · parfevalOnAll · parpool · redistribute · spmdBarrier · spmdBroadcast · spmdCat · spmdIndex · spmdPlus · spmdProbe · spmdReceive · spmdReduce · spmdSend · spmdSendReceive · spmdSize
Related guides
Source you can read
Every RunMat function's source is public. Read exactly how fetchNext is executed, line by line, in Rust.
- View the source for fetchNext in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.