Fibers Proposal (Need a chempion)

Pausable stackful coroutines (fibers) give ability to write simple but responsive and concurrent code.

It can be used for:

  • Automatic pause long task every 16 ms to ensure 60fps.
  • Concurrent execution of different tasks (not serial).
  • Abort incomplete task (with subtasks) as reaction on event.
  • Write simple (synchronous) code.
  • Improve performance because synchronous code can be better optimized by JIT.
  • Call synchronous API (like Array:reduce) without loosing of concurrency.


// Fiber - lightweight thread, that has separate call stack.
// Multiple fibers concurrently executes in one system thread.
declare class Fiber< Result > extends Promise< Result > {

	// Fiber that are executing now.
	static current : Fiber< any > | null

	// Freezes current fiber until promise will be resolved.
	// After, resumes fiber and returns result or rethrows an exception if promise is rejected.
	// Throws NotInFiberError if called outside any fiber.
	static wait< Result >( promise : PromiseLike< Result > ) : Result

	// Executes function in separate fiber
	static run( task : ()=> Result ) : Fiber< Result >

	// Abort execution.
	// Do nothing if is already completed.
	abort : ()=> void


Other Languages

  • Greenlets in Python
  • Goroutines in Go
  • Fibers in D
  • Virtual Thread in Loom in JVM


More info: JS Proposal: ยท GitHub

1 Like