or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

fiber-core.mdfuture.mdindex.md
tile.json

index.mddocs/

Fibers

Fibers provides cooperative multi-tasking for JavaScript through fibers (coroutines), enabling synchronous-style code to work in asynchronous environments. This library exposes an API to jump between multiple call stacks within a single thread, making it useful for adapting synchronous libraries to work in asynchronous contexts.

⚠️ DEPRECATION NOTICE: This package is considered obsolete by its author and is incompatible with Node.js v16+. Modern alternatives like async/await, Promises, and Generators are recommended instead.

Package Information

  • Package Name: fibers
  • Package Type: npm
  • Language: JavaScript (with native C++ components)
  • Installation: npm install fibers
  • Node.js Compatibility: v10.0.0 - v15.x (incompatible with v16+)

Core Imports

var Fiber = require('fibers');

For the Future abstraction:

var Future = require('fibers/future');

Basic Usage

var Fiber = require('fibers');

function sleep(ms) {
  var fiber = Fiber.current;
  setTimeout(function() {
    fiber.run();
  }, ms);
  Fiber.yield();
}

Fiber(function() {
  console.log('wait... ' + new Date);
  sleep(1000);
  console.log('ok... ' + new Date);
}).run();
console.log('back in main');

Architecture

Fibers is built around several key components:

  • Core Fiber API: Low-level fiber creation and control for cooperative multitasking
  • Future Abstraction: Higher-level Promise-like abstraction built on top of fibers
  • Native Implementation: Platform-specific binary compiled from C++ source code
  • Async Hooks Integration: Compatibility layer for Node.js async context tracking

Capabilities

Core Fiber Operations

Low-level fiber creation, execution control, and cooperative yielding. Essential for building custom async abstractions and implementing synchronous-style APIs over asynchronous operations.

/**
 * Creates a new fiber that executes the given function
 * @param {Function} fn - Function to execute in the fiber
 * @returns {Fiber} New fiber instance
 */
function Fiber(fn) { /* [native code] */ }

/**
 * Reference to currently executing fiber (undefined if on main stack)
 */
Fiber.current = undefined;

/**
 * Halts current fiber execution and returns control to caller
 * @param {*} param - Optional value to return to caller
 * @returns {*} Value passed to next run() call
 */
Fiber.yield = function(param) { /* [native code] */ }

Core Fiber Operations

Future-based Abstractions

Promise-like abstraction that wraps fiber operations in a more familiar async/await-style interface. Ideal for converting callback-based Node.js APIs to synchronous-style code.

/**
 * Future constructor for async operations
 */
function Future() {}

/**
 * Runs function in fiber context and returns future to its return value
 * @param {Function} fn - Function to run in fiber
 * @returns {Future} Future resolving to function's return value
 */
Future.task = function(fn) { /* ... */ }

/**
 * Wraps node-style async functions to return futures
 * @param {Function|Object} fnOrObject - Function or object to wrap
 * @param {boolean} multi - Whether callback returns multiple arguments
 * @param {string} suffix - Suffix for wrapped method names
 * @returns {Function|Object} Future-returning function or wrapped object
 */
Future.wrap = function(fnOrObject, multi, suffix) { /* ... */ }

Future Abstractions

Types

/**
 * Fiber class for cooperative multitasking
 */
class Fiber {
  /**
   * Starts or resumes fiber execution
   * @param {*} param - Optional parameter to pass to fiber
   * @returns {*} Value passed to yield() or fiber return value
   */
  run(param) { /* [native code] */ }
  
  /**
   * Terminates running fiber and restores to original state
   * @returns {*} Return value from fiber if any
   */
  reset() { /* [native code] */ }
  
  /**
   * Causes yielding fiber's yield() to throw instead of return
   * @param {Error} exception - Exception to throw in fiber
   */
  throwInto(exception) { /* [native code] */ }
}

/**
 * Future class for Promise-like async operations
 */
class Future {
  /**
   * Returns future value (throws if not resolved)
   * @returns {*} The resolved value
   */
  get() { /* ... */ }
  
  /**
   * Resolves future with value
   * @param {*} value - Value to resolve with
   */
  return(value) { /* ... */ }
  
  /**
   * Rejects future with error
   * @param {Error} error - Error to reject with
   */
  throw(error) { /* ... */ }
  
  /**
   * Waits for future to resolve and returns value
   * @returns {*} The resolved value
   */
  wait() { /* ... */ }
  
  /**
   * Returns boolean indicating if future is resolved
   * @returns {boolean} True if resolved, false otherwise
   */
  isResolved() { /* ... */ }
  
  /**
   * Propagate only errors to another future or array of futures
   * @param {Future|Future[]} futures - Target future(s) for error propagation
   * @returns {Future} This future for chaining
   */
  proxyErrors(futures) { /* ... */ }
}