← index

Fiber

inherits Object

A coroutine: a block that can suspend itself with Fiber.yield: -- or the ^> value sugar -- and be resumed by its caller, exchanging values both ways. Cooperative and in-task; for concurrent tasks whose I/O overlaps, use Task / Async instead.

var f = Fiber.new:{ ^> 1; 2 };
#( f.resume f.resume f.status )    "* -> #(1 2 done)

Class methods

current

The currently running fiber, or nil outside any fiber.

native

new:

A fresh, unstarted fiber wrapping the zero-parameter block (status 'created'). Nothing runs until the first resume.

native

yield

As yield: with nil.

native

yield:

Suspend the running fiber, delivering the value as the pending resume's result; the value a later resume: passes becomes this expression's value. ^> value is the yield sugar.

var f = Fiber.new:{ var got = ^> 1; got * 10 };
f.resume;     "* -> 1
f.resume:5    "* -> 50

native

Instance methods

alive?

True while the fiber can still be resumed (not yet done or failed).

native

done?

True once the fiber's block returned normally.

native

error

The error value if the fiber failed; nil otherwise.

native

failed?

True once the fiber's block raised an uncaught error (the error re-raised at the resume that observed it; error keeps the value).

native

result

The fiber's final return value; nil unless it completed normally.

native

resume

Run the fiber until its next yield or completion; answers the yielded (or final) value. If the fiber's block raised, the error re-raises here. Resuming a finished fiber -- or one live in another task -- raises a FiberError.

native

resume:

As resume, delivering the value as the result of the yield expression the fiber is suspended at.

native

status

One of 'created', 'suspended', 'running', 'done', 'failed'.

(Fiber.new:{ }).status    "* -> created

native